blob: 194bf0a9604074b31f192255523e208c484268df [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/* This file contains codegen for the Thumb2 ISA. */
18
19#include "arm_lir.h"
20#include "codegen_arm.h"
21#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "mirror/array.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
buzbee2700f7e2014-03-07 09:46:20 -080027LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070028 OpRegReg(kOpCmp, src1, src2);
29 return OpCondBranch(cond, target);
30}
31
32/*
33 * Generate a Thumb2 IT instruction, which can nullify up to
34 * four subsequent instructions based on a condition and its
35 * inverse. The condition applies to the first instruction, which
36 * is executed if the condition is met. The string "guide" consists
37 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
38 * A "T" means the instruction is executed if the condition is
39 * met, and an "E" means the instruction is executed if the condition
40 * is not met.
41 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070042LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 int mask;
44 int mask3 = 0;
45 int mask2 = 0;
46 int mask1 = 0;
47 ArmConditionCode code = ArmConditionEncoding(ccode);
48 int cond_bit = code & 1;
49 int alt_bit = cond_bit ^ 1;
50
Brian Carlstrom7934ac22013-07-26 10:54:15 -070051 // Note: case fallthroughs intentional
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 switch (strlen(guide)) {
53 case 3:
54 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
55 case 2:
56 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
57 case 1:
58 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
59 break;
60 case 0:
61 break;
62 default:
63 LOG(FATAL) << "OAT: bad case in OpIT";
64 }
65 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
66 (1 << (3 - strlen(guide)));
67 return NewLIR2(kThumb2It, code, mask);
68}
69
70/*
71 * 64-bit 3way compare function.
72 * mov rX, #-1
73 * cmp op1hi, op2hi
74 * blt done
75 * bgt flip
76 * sub rX, op1lo, op2lo (treat as unsigned)
77 * beq done
78 * ite hi
79 * mov(hi) rX, #-1
80 * mov(!hi) rX, #1
81 * flip:
82 * neg rX
83 * done:
84 */
85void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070086 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 LIR* target1;
88 LIR* target2;
89 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
90 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080091 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -080093 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 LIR* branch1 = OpCondBranch(kCondLt, NULL);
95 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbee2700f7e2014-03-07 09:46:20 -080096 OpRegRegReg(kOpSub, t_reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 LIR* branch3 = OpCondBranch(kCondEq, NULL);
98
99 OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800100 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 LoadConstant(t_reg, 1);
102 GenBarrier();
103
104 target2 = NewLIR0(kPseudoTargetLabel);
105 OpRegReg(kOpNeg, t_reg, t_reg);
106
107 target1 = NewLIR0(kPseudoTargetLabel);
108
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700109 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800110 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 StoreValue(rl_dest, rl_temp);
112 FreeTemp(t_reg);
113
114 branch1->target = target1;
115 branch2->target = target2;
116 branch3->target = branch1->target;
117}
118
119void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700120 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 int32_t val_lo = Low32Bits(val);
122 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700123 DCHECK_GE(ModifiedImmediate(val_lo), 0);
124 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700125 LIR* taken = &block_label_list_[bb->taken];
126 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800128 RegStorage low_reg = rl_src1.reg.GetLow();
129 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
Vladimir Marko58af1f92013-12-19 13:31:15 +0000131 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800132 RegStorage t_reg = AllocTemp();
133 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000134 FreeTemp(t_reg);
135 OpCondBranch(ccode, taken);
136 return;
137 }
138
Brian Carlstromdf629502013-07-17 22:39:56 -0700139 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 case kCondEq:
141 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000142 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 break;
144 case kCondLt:
145 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
146 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000147 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 break;
149 case kCondLe:
150 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
151 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
152 ccode = kCondLs;
153 break;
154 case kCondGt:
155 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
156 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
157 ccode = kCondHi;
158 break;
159 case kCondGe:
160 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
161 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000162 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 break;
164 default:
165 LOG(FATAL) << "Unexpected ccode: " << ccode;
166 }
167 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
168}
169
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700170void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 RegLocation rl_result;
172 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 RegLocation rl_dest = mir_graph_->GetDest(mir);
174 rl_src = LoadValue(rl_src, kCoreReg);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000175 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 if (mir->ssa_rep->num_uses == 1) {
177 // CONST case
178 int true_val = mir->dalvikInsn.vB;
179 int false_val = mir->dalvikInsn.vC;
180 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000181 // Change kCondNe to kCondEq for the special cases below.
182 if (ccode == kCondNe) {
183 ccode = kCondEq;
184 std::swap(true_val, false_val);
185 }
186 bool cheap_false_val = InexpensiveConstantInt(false_val);
187 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800188 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000189 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
190 OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800191 LoadConstant(rl_result.reg, false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700192 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000193 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800194 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000195 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
196 OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800197 LoadConstant(rl_result.reg, false_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000198 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
199 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800200 OpRegImm(kOpCmp, rl_src.reg, 0);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000201 OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800202 LoadConstant(rl_result.reg, true_val);
203 LoadConstant(rl_result.reg, false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700204 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 } else {
206 // Unlikely case - could be tuned.
buzbee2700f7e2014-03-07 09:46:20 -0800207 RegStorage t_reg1 = AllocTemp();
208 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 LoadConstant(t_reg1, true_val);
210 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800211 OpRegImm(kOpCmp, rl_src.reg, 0);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000212 OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800213 OpRegCopy(rl_result.reg, t_reg1);
214 OpRegCopy(rl_result.reg, t_reg2);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700215 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 }
217 } else {
218 // MOVE case
219 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
220 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
221 rl_true = LoadValue(rl_true, kCoreReg);
222 rl_false = LoadValue(rl_false, kCoreReg);
223 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800224 OpRegImm(kOpCmp, rl_src.reg, 0);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000225 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Vladimir Markoa1a70742014-03-03 10:28:05 +0000226 OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800227 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000228 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Vladimir Markoa1a70742014-03-03 10:28:05 +0000229 OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800230 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700231 } else { // Normal - select between the two.
Vladimir Markoa1a70742014-03-03 10:28:05 +0000232 OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800233 OpRegCopy(rl_result.reg, rl_true.reg);
234 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700235 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700236 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 }
238 StoreValue(rl_dest, rl_result);
239}
240
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700241void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
243 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
244 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000245 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000247 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 ccode = FlipComparisonOrder(ccode);
249 }
250 if (rl_src2.is_const) {
251 RegLocation rl_temp = UpdateLocWide(rl_src2);
252 // Do special compare/branch against simple const operand if not already in registers.
253 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
254 if ((rl_temp.location != kLocPhysReg) &&
255 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
256 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
257 return;
258 }
259 }
buzbee0d829482013-10-11 15:24:55 -0700260 LIR* taken = &block_label_list_[bb->taken];
261 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
263 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800264 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700265 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 case kCondEq:
267 OpCondBranch(kCondNe, not_taken);
268 break;
269 case kCondNe:
270 OpCondBranch(kCondNe, taken);
271 break;
272 case kCondLt:
273 OpCondBranch(kCondLt, taken);
274 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000275 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 break;
277 case kCondLe:
278 OpCondBranch(kCondLt, taken);
279 OpCondBranch(kCondGt, not_taken);
280 ccode = kCondLs;
281 break;
282 case kCondGt:
283 OpCondBranch(kCondGt, taken);
284 OpCondBranch(kCondLt, not_taken);
285 ccode = kCondHi;
286 break;
287 case kCondGe:
288 OpCondBranch(kCondGt, taken);
289 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000290 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 break;
292 default:
293 LOG(FATAL) << "Unexpected ccode: " << ccode;
294 }
buzbee2700f7e2014-03-07 09:46:20 -0800295 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 OpCondBranch(ccode, taken);
297}
298
299/*
300 * Generate a register comparison to an immediate and branch. Caller
301 * is responsible for setting branch target field.
302 */
buzbee2700f7e2014-03-07 09:46:20 -0800303LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700306 /*
307 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
308 * compare-and-branch if zero is ideal if it will reach. However, because null checks
309 * branch forward to a launch pad, they will frequently not reach - and thus have to
310 * be converted to a long form during assembly (which will trigger another assembly
311 * pass). Here we estimate the branch distance for checks, and if large directly
312 * generate the long form in an attempt to avoid an extra assembly pass.
313 * TODO: consider interspersing launchpads in code following unconditional branches.
314 */
315 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
316 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
buzbee2700f7e2014-03-07 09:46:20 -0800317 if (!skip && (ARM_LOWREG(reg.GetReg())) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
319 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
buzbee2700f7e2014-03-07 09:46:20 -0800320 reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000322 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 branch = NewLIR2(kThumbBCond, 0, arm_cond);
324 }
325 branch->target = target;
326 return branch;
327}
328
buzbee2700f7e2014-03-07 09:46:20 -0800329LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 LIR* res;
331 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800332 // If src or dest is a pair, we'll be using low reg.
333 if (r_dest.IsPair()) {
334 r_dest = r_dest.GetLow();
335 }
336 if (r_src.IsPair()) {
337 r_src = r_src.GetLow();
338 }
339 if (ARM_FPREG(r_dest.GetReg()) || ARM_FPREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 return OpFpRegCopy(r_dest, r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800341 if (ARM_LOWREG(r_dest.GetReg()) && ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 opcode = kThumbMovRR;
buzbee2700f7e2014-03-07 09:46:20 -0800343 else if (!ARM_LOWREG(r_dest.GetReg()) && !ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 opcode = kThumbMovRR_H2H;
buzbee2700f7e2014-03-07 09:46:20 -0800345 else if (ARM_LOWREG(r_dest.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 opcode = kThumbMovRR_H2L;
347 else
348 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800349 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
351 res->flags.is_nop = true;
352 }
353 return res;
354}
355
buzbee2700f7e2014-03-07 09:46:20 -0800356LIR* ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
358 AppendLIR(res);
359 return res;
360}
361
buzbee2700f7e2014-03-07 09:46:20 -0800362void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
363 bool dest_fp = ARM_FPREG(r_dest.GetLowReg());
364 bool src_fp = ARM_FPREG(r_src.GetLowReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 if (dest_fp) {
366 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800367 // FIXME: handle 64-bit solo's here.
368 OpRegCopy(RegStorage::Solo64(S2d(r_dest.GetLowReg(), r_dest.GetHighReg())),
369 RegStorage::Solo64(S2d(r_src.GetLowReg(), r_src.GetHighReg())));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800371 NewLIR3(kThumb2Fmdrr, S2d(r_dest.GetLowReg(), r_dest.GetHighReg()),
372 r_src.GetLowReg(), r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
374 } else {
375 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), S2d(r_src.GetLowReg(),
377 r_src.GetHighReg()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 } else {
379 // Handle overlap
buzbee2700f7e2014-03-07 09:46:20 -0800380 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
381 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
382 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
383 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800385 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
386 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 }
388 }
389 }
390}
391
392// Table of magic divisors
393struct MagicTable {
394 uint32_t magic;
395 uint32_t shift;
396 DividePattern pattern;
397};
398
399static const MagicTable magic_table[] = {
400 {0, 0, DivideNone}, // 0
401 {0, 0, DivideNone}, // 1
402 {0, 0, DivideNone}, // 2
403 {0x55555556, 0, Divide3}, // 3
404 {0, 0, DivideNone}, // 4
405 {0x66666667, 1, Divide5}, // 5
406 {0x2AAAAAAB, 0, Divide3}, // 6
407 {0x92492493, 2, Divide7}, // 7
408 {0, 0, DivideNone}, // 8
409 {0x38E38E39, 1, Divide5}, // 9
410 {0x66666667, 2, Divide5}, // 10
411 {0x2E8BA2E9, 1, Divide5}, // 11
412 {0x2AAAAAAB, 1, Divide5}, // 12
413 {0x4EC4EC4F, 2, Divide5}, // 13
414 {0x92492493, 3, Divide7}, // 14
415 {0x88888889, 3, Divide7}, // 15
416};
417
418// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700419bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700420 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
422 return false;
423 }
424 DividePattern pattern = magic_table[lit].pattern;
425 if (pattern == DivideNone) {
426 return false;
427 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428
buzbee2700f7e2014-03-07 09:46:20 -0800429 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 LoadConstant(r_magic, magic_table[lit].magic);
431 rl_src = LoadValue(rl_src, kCoreReg);
432 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800433 RegStorage r_hi = AllocTemp();
434 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100435
436 // rl_dest and rl_src might overlap.
437 // Reuse r_hi to save the div result for reminder case.
438 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
439
buzbee2700f7e2014-03-07 09:46:20 -0800440 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700441 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100443 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 break;
445 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800446 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100447 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700448 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 break;
450 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800451 OpRegReg(kOpAdd, r_hi, rl_src.reg);
452 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100453 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700454 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 break;
456 default:
457 LOG(FATAL) << "Unexpected pattern: " << pattern;
458 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100459
460 if (!is_div) {
461 // div_result = src / lit
462 // tmp1 = div_result * lit
463 // dest = src - tmp1
464 RegStorage tmp1 = r_lo;
465 EasyMultiplyOp ops[2];
466
467 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
468 DCHECK_NE(canEasyMultiply, false);
469
470 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
471 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
472 }
473
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 StoreValue(rl_dest, rl_result);
475 return true;
476}
477
Ian Rogerse2143c02014-03-28 08:47:16 -0700478// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
479bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
480 if (IsPowerOfTwo(lit)) {
481 op->op = kOpLsl;
482 op->shift = LowestSetBit(lit);
483 return true;
484 }
485
486 if (IsPowerOfTwo(lit - 1)) {
487 op->op = kOpAdd;
488 op->shift = LowestSetBit(lit - 1);
489 return true;
490 }
491
492 if (IsPowerOfTwo(lit + 1)) {
493 op->op = kOpRsub;
494 op->shift = LowestSetBit(lit + 1);
495 return true;
496 }
497
498 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100499 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700500 return false;
501}
502
503// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
504bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
505 GetEasyMultiplyOp(lit, &ops[0]);
506 if (GetEasyMultiplyOp(lit, &ops[0])) {
507 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100508 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700509 return true;
510 }
511
512 int lit1 = lit;
513 uint32_t shift = LowestSetBit(lit1);
514 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
515 ops[1].op = kOpLsl;
516 ops[1].shift = shift;
517 return true;
518 }
519
520 lit1 = lit - 1;
521 shift = LowestSetBit(lit1);
522 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
523 ops[1].op = kOpAdd;
524 ops[1].shift = shift;
525 return true;
526 }
527
528 lit1 = lit + 1;
529 shift = LowestSetBit(lit1);
530 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
531 ops[1].op = kOpRsub;
532 ops[1].shift = shift;
533 return true;
534 }
535
536 return false;
537}
538
Zheng Xuf9719f92014-04-02 13:31:31 +0100539// Generate instructions to do multiply.
540// Additional temporary register is required,
541// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700542void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100543 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
544 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
545
546 RegStorage r_tmp1;
547 if (ops[1].op == kOpInvalid) {
548 r_tmp1 = r_dest;
549 } else if (r_dest.GetReg() != r_src.GetReg()) {
550 r_tmp1 = r_dest;
551 } else {
552 r_tmp1 = AllocTemp();
553 }
554
555 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700556 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100557 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700558 break;
559 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100560 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700561 break;
562 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100563 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700564 break;
565 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100566 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700567 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100568 }
569
570 switch (ops[1].op) {
571 case kOpInvalid:
572 return;
573 case kOpLsl:
574 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
575 break;
576 case kOpAdd:
577 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
578 break;
579 case kOpRsub:
580 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
581 break;
582 default:
583 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
584 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700585 }
586}
587
588bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
589 EasyMultiplyOp ops[2];
590
591 if (!GetEasyMultiplyTwoOps(lit, ops)) {
592 return false;
593 }
594
595 rl_src = LoadValue(rl_src, kCoreReg);
596 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
597
598 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
599 StoreValue(rl_dest, rl_result);
600 return true;
601}
602
buzbee2700f7e2014-03-07 09:46:20 -0800603LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code, RegStorage reg1, RegStorage base,
604 int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
606 return NULL;
607}
608
Mark Mendell2bf31e62014-01-23 12:13:40 -0800609RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
610 RegLocation rl_src2, bool is_div, bool check_zero) {
611 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
612 return rl_dest;
613}
614
615RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
616 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
617 return rl_dest;
618}
619
buzbee2700f7e2014-03-07 09:46:20 -0800620RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700621 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
622
623 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800624 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700625 LoadConstant(lit_temp, lit);
626 // Use the generic case for div/rem with arg2 in a register.
627 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
628 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
629 FreeTemp(lit_temp);
630
631 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632}
633
buzbee2700f7e2014-03-07 09:46:20 -0800634RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700635 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700636 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
637 if (is_div) {
638 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800639 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700640 } else {
641 // Remainder case, use the following code:
642 // temp = reg1 / reg2 - integer division
643 // temp = temp * reg2
644 // dest = reg1 - temp
645
buzbee2700f7e2014-03-07 09:46:20 -0800646 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700647 OpRegRegReg(kOpDiv, temp, reg1, reg2);
648 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800649 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700650 FreeTemp(temp);
651 }
652
653 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654}
655
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700656bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 DCHECK_EQ(cu_->instruction_set, kThumb2);
658 RegLocation rl_src1 = info->args[0];
659 RegLocation rl_src2 = info->args[1];
660 rl_src1 = LoadValue(rl_src1, kCoreReg);
661 rl_src2 = LoadValue(rl_src2, kCoreReg);
662 RegLocation rl_dest = InlineTarget(info);
663 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800664 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800666 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
667 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 GenBarrier();
669 StoreValue(rl_dest, rl_result);
670 return true;
671}
672
Vladimir Markoe508a202013-11-04 15:24:22 +0000673bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
674 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800675 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000676 RegLocation rl_dest = InlineTarget(info);
677 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
678 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
679 if (size == kLong) {
680 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800681 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
682 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
683 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000684 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800685 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
686 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000687 }
688 StoreValueWide(rl_dest, rl_result);
689 } else {
690 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
691 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800692 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000693 StoreValue(rl_dest, rl_result);
694 }
695 return true;
696}
697
698bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
699 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800700 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000701 RegLocation rl_src_value = info->args[2]; // [size] value
702 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
703 if (size == kLong) {
704 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
705 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800706 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), kWord);
707 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000708 } else {
709 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
710 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
711 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800712 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000713 }
714 return true;
715}
716
buzbee2700f7e2014-03-07 09:46:20 -0800717void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 LOG(FATAL) << "Unexpected use of OpLea for Arm";
719}
720
Ian Rogersdd7624d2014-03-14 17:43:00 -0700721void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
723}
724
Vladimir Marko1c282e22013-11-21 14:49:47 +0000725bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 DCHECK_EQ(cu_->instruction_set, kThumb2);
727 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000728 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
729 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800730 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000731 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000732 // If is_long, high half is in info->args[5]
733 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
734 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 RegLocation rl_dest = InlineTarget(info); // boolean place for result
736
Vladimir Marko3e5af822013-11-21 15:01:20 +0000737 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
738 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
739 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
740 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
741 // into the same temps, reducing the number of required temps down to 5. We shall work
742 // around the potentially locked temp by using LR for r_ptr, unconditionally.
743 // TODO: Pass information about the need for more temps to the stack frame generation
744 // code so that we can rely on being able to allocate enough temps.
745 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
746 MarkTemp(rARM_LR);
747 FreeTemp(rARM_LR);
748 LockTemp(rARM_LR);
749 bool load_early = true;
750 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800751 int expected_reg = is_long ? rl_src_expected.reg.GetLowReg() : rl_src_expected.reg.GetReg();
752 int new_val_reg = is_long ? rl_src_new_value.reg.GetLowReg() : rl_src_new_value.reg.GetReg();
753 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !IsFpReg(expected_reg);
754 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !IsFpReg(new_val_reg);
755 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
756 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000757
758 if (!expected_is_good_reg && !new_value_is_good_reg) {
759 // None of expected/new_value is non-temp reg, need to load both late
760 load_early = false;
761 // Make sure they are not in the temp regs and the load will not be skipped.
762 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800763 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000764 ClobberSReg(rl_src_expected.s_reg_low);
765 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
766 rl_src_expected.location = kLocDalvikFrame;
767 }
768 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800769 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000770 ClobberSReg(rl_src_new_value.s_reg_low);
771 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
772 rl_src_new_value.location = kLocDalvikFrame;
773 }
774 }
775 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776
777 // Release store semantics, get the barrier out of the way. TODO: revisit
778 GenMemBarrier(kStoreLoad);
779
780 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000781 RegLocation rl_new_value;
782 if (!is_long) {
783 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
784 } else if (load_early) {
785 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
786 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787
Vladimir Marko1c282e22013-11-21 14:49:47 +0000788 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800790 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 }
792
793 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
794
buzbee2700f7e2014-03-07 09:46:20 -0800795 RegStorage r_ptr = rs_rARM_LR;
796 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797
798 // Free now unneeded rl_object and rl_offset to give more temps.
799 ClobberSReg(rl_object.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000800 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000802 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803
Vladimir Marko3e5af822013-11-21 15:01:20 +0000804 RegLocation rl_expected;
805 if (!is_long) {
806 rl_expected = LoadValue(rl_src_expected, kCoreReg);
807 } else if (load_early) {
808 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
809 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000810 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee2700f7e2014-03-07 09:46:20 -0800811 int low_reg = AllocTemp().GetReg();
812 int high_reg = AllocTemp().GetReg();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000813 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
814 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000815 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816
Vladimir Marko3e5af822013-11-21 15:01:20 +0000817 // do {
818 // tmp = [r_ptr] - expected;
819 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
820 // result = tmp != 0;
821
buzbee2700f7e2014-03-07 09:46:20 -0800822 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700823 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700824
Vladimir Marko3e5af822013-11-21 15:01:20 +0000825 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800826 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000827 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800828 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000829 }
buzbee2700f7e2014-03-07 09:46:20 -0800830 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
831 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
832 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000833 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800834 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000835 }
836 // Make sure we use ORR that sets the ccode
buzbee2700f7e2014-03-07 09:46:20 -0800837 if (ARM_LOWREG(r_tmp.GetReg()) && ARM_LOWREG(r_tmp_high.GetReg())) {
838 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000839 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800840 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000841 }
842 FreeTemp(r_tmp_high); // Now unneeded
843
844 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
845 OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800846 NewLIR4(kThumb2Strexd /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetLowReg(), rl_new_value.reg.GetHighReg(), r_ptr.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000847
848 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800849 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
850 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000851 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
852 OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800853 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000854 }
855
856 // Still one conditional left from OpIT(kCondEq, "T") from either branch
857 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison43a065c2014-04-01 15:14:46 -0700858 GenBarrier();
859
Jeff Hao2de2aa12013-09-12 17:20:31 -0700860 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861
Vladimir Marko3e5af822013-11-21 15:01:20 +0000862 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800863 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000864 }
865
866 // result := (tmp1 != 0) ? 0 : 1;
867 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800868 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000869 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000870 OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800871 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000872 FreeTemp(r_tmp); // Now unneeded.
Dave Allison43a065c2014-04-01 15:14:46 -0700873 GenBarrier(); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000874
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 StoreValue(rl_dest, rl_result);
876
Vladimir Marko3e5af822013-11-21 15:01:20 +0000877 // Now, restore lr to its non-temp status.
878 Clobber(rARM_LR);
879 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 return true;
881}
882
buzbee2700f7e2014-03-07 09:46:20 -0800883LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
884 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885}
886
buzbee2700f7e2014-03-07 09:46:20 -0800887LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
888 return NewLIR3(kThumb2Vldms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889}
890
buzbee2700f7e2014-03-07 09:46:20 -0800891LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
892 return NewLIR3(kThumb2Vstms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893}
894
895void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
896 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700898 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 EncodeShift(kArmLsl, second_bit - first_bit));
900 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800901 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 }
903}
904
buzbee2700f7e2014-03-07 09:46:20 -0800905void ArmMir2Lir::GenDivZeroCheck(RegStorage reg) {
906 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
907 RegStorage t_reg = AllocTemp();
908 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 FreeTemp(t_reg);
910 GenCheck(kCondEq, kThrowDivZero);
911}
912
913// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700914LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
916 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
917}
918
919// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800920LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000922 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
923 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 return OpCondBranch(c_code, target);
925}
926
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700927void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800929 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
930 LIR* barrier = last_lir_insn_;
931
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 int dmb_flavor;
933 // TODO: revisit Arm barrier kinds
934 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800935 case kLoadStore: dmb_flavor = kISH; break;
936 case kLoadLoad: dmb_flavor = kISH; break;
937 case kStoreStore: dmb_flavor = kISHST; break;
938 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 default:
940 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
941 dmb_flavor = kSY; // quiet gcc.
942 break;
943 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800944
945 // If the same barrier already exists, don't generate another.
946 if (barrier == nullptr
947 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
948 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
949 }
950
951 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
952 DCHECK(!barrier->flags.use_def_invalid);
953 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954#endif
955}
956
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700957void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 rl_src = LoadValueWide(rl_src, kCoreReg);
959 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800960 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 LoadConstantNoClobber(z_reg, 0);
962 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -0800963 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
964 RegStorage t_reg = AllocTemp();
965 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
966 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 FreeTemp(t_reg);
968 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800969 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
970 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 }
972 FreeTemp(z_reg);
973 StoreValueWide(rl_dest, rl_result);
974}
975
Mark Mendelle02d48f2014-01-15 11:19:23 -0800976void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
977 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 /*
Zheng Xud7f8e022014-03-13 13:40:30 +0000979 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
980 * dest = src1.lo * src2.lo;
981 * tmp1 += src1.lo * src2.hi;
982 * dest.hi += tmp1;
983 *
984 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 * registers. Normally for Arm, we get 5. We can get to 6 by including
986 * lr in the temp set. The only problematic case is all operands and result are
987 * distinct, and none have been promoted. In that case, we can succeed by aggressively
988 * freeing operand temp registers after they are no longer needed. All other cases
989 * can proceed normally. We'll just punt on the case of the result having a misaligned
990 * overlap with either operand and send that case to a runtime handler.
991 */
992 RegLocation rl_result;
993 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700994 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 FlushAllRegs();
996 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
997 rl_result = GetReturnWide(false);
998 StoreValueWide(rl_dest, rl_result);
999 return;
1000 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001001
1002 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1003 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1004
1005 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001006 RegStorage res_lo;
1007 RegStorage res_hi;
1008 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1009 !IsTemp(rl_dest.reg.GetLowReg()) && !IsTemp(rl_dest.reg.GetHighReg());
1010 bool src1_promoted = !IsTemp(rl_src1.reg.GetLowReg()) && !IsTemp(rl_src1.reg.GetHighReg());
1011 bool src2_promoted = !IsTemp(rl_src2.reg.GetLowReg()) && !IsTemp(rl_src2.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001012 // Check if rl_dest is *not* either operand and we have enough temp registers.
1013 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1014 (dest_promoted || src1_promoted || src2_promoted)) {
1015 // In this case, we do not need to manually allocate temp registers for result.
1016 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001017 res_lo = rl_result.reg.GetLow();
1018 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001019 } else {
1020 res_lo = AllocTemp();
1021 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1022 // In this case, we have enough temp registers to be allocated for result.
1023 res_hi = AllocTemp();
1024 reg_status = 1;
1025 } else {
1026 // In this case, all temps are now allocated.
1027 // res_hi will be allocated after we can free src1_hi.
1028 reg_status = 2;
1029 }
1030 }
1031
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 // Temporarily add LR to the temp pool, and assign it to tmp1
1033 MarkTemp(rARM_LR);
1034 FreeTemp(rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001035 RegStorage tmp1 = rs_rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 LockTemp(rARM_LR);
1037
buzbee2700f7e2014-03-07 09:46:20 -08001038 if (rl_src1.reg == rl_src2.reg) {
1039 DCHECK(res_hi.Valid());
1040 DCHECK(res_lo.Valid());
1041 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1042 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1043 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001044 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001046 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001047 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001048 DCHECK(!res_hi.Valid());
1049 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001050 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1051 FreeTemp(rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001052 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 }
buzbee2700f7e2014-03-07 09:46:20 -08001054 DCHECK(res_hi.Valid());
1055 DCHECK(res_lo.Valid());
1056 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1057 rl_src1.reg.GetLowReg());
1058 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1059 tmp1.GetReg());
1060 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001061 if (reg_status == 2) {
1062 // Clobber rl_src1 since it was corrupted.
buzbee2700f7e2014-03-07 09:46:20 -08001063 FreeTemp(rl_src1.reg);
1064 Clobber(rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 }
1066 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001067
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001069 FreeTemp(tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 Clobber(rARM_LR);
1071 UnmarkTemp(rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001072
1073 if (reg_status != 0) {
1074 // We had manually allocated registers for rl_result.
1075 // Now construct a RegLocation.
1076 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001077 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001078 }
1079
1080 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081}
1082
Mark Mendelle02d48f2014-01-15 11:19:23 -08001083void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001084 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1086}
1087
Mark Mendelle02d48f2014-01-15 11:19:23 -08001088void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001089 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1091}
1092
Mark Mendelle02d48f2014-01-15 11:19:23 -08001093void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1096}
1097
Mark Mendelle02d48f2014-01-15 11:19:23 -08001098void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1101}
1102
Mark Mendelle02d48f2014-01-15 11:19:23 -08001103void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001104 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1106}
1107
1108/*
1109 * Generate array load
1110 */
1111void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001112 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 RegisterClass reg_class = oat_reg_class_by_size(size);
1114 int len_offset = mirror::Array::LengthOffset().Int32Value();
1115 int data_offset;
1116 RegLocation rl_result;
1117 bool constant_index = rl_index.is_const;
1118 rl_array = LoadValue(rl_array, kCoreReg);
1119 if (!constant_index) {
1120 rl_index = LoadValue(rl_index, kCoreReg);
1121 }
1122
1123 if (rl_dest.wide) {
1124 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1125 } else {
1126 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1127 }
1128
1129 // If index is constant, just fold it into the data offset
1130 if (constant_index) {
1131 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1132 }
1133
1134 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001135 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136
1137 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001138 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 if (needs_range_check) {
1140 reg_len = AllocTemp();
1141 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001142 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001143 MarkPossibleNullPointerException(opt_flags);
1144 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001145 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 }
1147 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001148 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001150 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 } else {
1152 // No special indexed operation, lea + load w/ displacement
1153 reg_ptr = AllocTemp();
Ian Rogerse2143c02014-03-28 08:47:16 -07001154 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001155 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157 rl_result = EvalLoc(rl_dest, reg_class, true);
1158
1159 if (needs_range_check) {
1160 if (constant_index) {
1161 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1162 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001163 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 FreeTemp(reg_len);
1166 }
1167 if (rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001168 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001169 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 if (!constant_index) {
1171 FreeTemp(reg_ptr);
1172 }
1173 StoreValueWide(rl_dest, rl_result);
1174 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001175 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001176 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 if (!constant_index) {
1178 FreeTemp(reg_ptr);
1179 }
1180 StoreValue(rl_dest, rl_result);
1181 }
1182 } else {
1183 // Offset base, then use indexed load
buzbee2700f7e2014-03-07 09:46:20 -08001184 RegStorage reg_ptr = AllocTemp();
1185 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001186 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 rl_result = EvalLoc(rl_dest, reg_class, true);
1188
1189 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001190 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 FreeTemp(reg_len);
1192 }
buzbee2700f7e2014-03-07 09:46:20 -08001193 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001194 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 FreeTemp(reg_ptr);
1196 StoreValue(rl_dest, rl_result);
1197 }
1198}
1199
1200/*
1201 * Generate array store
1202 *
1203 */
1204void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001205 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 RegisterClass reg_class = oat_reg_class_by_size(size);
1207 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 bool constant_index = rl_index.is_const;
1209
Ian Rogersa9a82542013-10-04 11:17:26 -07001210 int data_offset;
1211 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1213 } else {
1214 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1215 }
1216
1217 // If index is constant, just fold it into the data offset.
1218 if (constant_index) {
1219 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1220 }
1221
1222 rl_array = LoadValue(rl_array, kCoreReg);
1223 if (!constant_index) {
1224 rl_index = LoadValue(rl_index, kCoreReg);
1225 }
1226
buzbee2700f7e2014-03-07 09:46:20 -08001227 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001228 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001230 reg_ptr = rl_array.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001231 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1232 Clobber(rl_array.reg.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001233 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001235 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 reg_ptr = AllocTemp();
1237 }
1238
1239 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001240 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241
1242 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001243 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 if (needs_range_check) {
1245 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001246 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001248 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001249 MarkPossibleNullPointerException(opt_flags);
1250 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001251 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 }
1253 /* at this point, reg_ptr points to array, 2 live temps */
1254 if (rl_src.wide || rl_src.fp || constant_index) {
1255 if (rl_src.wide) {
1256 rl_src = LoadValueWide(rl_src, reg_class);
1257 } else {
1258 rl_src = LoadValue(rl_src, reg_class);
1259 }
1260 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001261 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 }
1263 if (needs_range_check) {
1264 if (constant_index) {
1265 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1266 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001267 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 }
1269 FreeTemp(reg_len);
1270 }
1271
1272 if (rl_src.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001273 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001275 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 }
Dave Allisonb373e092014-02-20 16:06:36 -08001277 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 } else {
1279 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001280 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 rl_src = LoadValue(rl_src, reg_class);
1282 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001283 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 FreeTemp(reg_len);
1285 }
buzbee2700f7e2014-03-07 09:46:20 -08001286 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001287 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 }
Ian Rogers773aab12013-10-14 13:50:10 -07001289 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 FreeTemp(reg_ptr);
1291 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001292 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001293 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 }
1295}
1296
Ian Rogersa9a82542013-10-04 11:17:26 -07001297
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001299 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 rl_src = LoadValueWide(rl_src, kCoreReg);
1301 // Per spec, we only care about low 6 bits of shift amount.
1302 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1303 if (shift_amount == 0) {
1304 StoreValueWide(rl_dest, rl_src);
1305 return;
1306 }
1307 if (BadOverlap(rl_src, rl_dest)) {
1308 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1309 return;
1310 }
1311 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001312 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 case Instruction::SHL_LONG:
1314 case Instruction::SHL_LONG_2ADDR:
1315 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001316 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1317 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001319 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1320 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001322 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1323 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001325 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001326 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001328 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 }
1330 break;
1331 case Instruction::SHR_LONG:
1332 case Instruction::SHR_LONG_2ADDR:
1333 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001334 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1335 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001337 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1338 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001340 RegStorage t_reg = AllocTemp();
1341 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001342 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 EncodeShift(kArmLsl, 32 - shift_amount));
1344 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001345 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 }
1347 break;
1348 case Instruction::USHR_LONG:
1349 case Instruction::USHR_LONG_2ADDR:
1350 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001351 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1352 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001354 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1355 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001357 RegStorage t_reg = AllocTemp();
1358 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001359 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 EncodeShift(kArmLsl, 32 - shift_amount));
1361 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001362 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 }
1364 break;
1365 default:
1366 LOG(FATAL) << "Unexpected case";
1367 }
1368 StoreValueWide(rl_dest, rl_result);
1369}
1370
1371void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001372 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1374 if (!rl_src2.is_const) {
1375 // Don't bother with special handling for subtract from immediate.
1376 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1377 return;
1378 }
1379 } else {
1380 // Normalize
1381 if (!rl_src2.is_const) {
1382 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001383 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 }
1385 }
1386 if (BadOverlap(rl_src1, rl_dest)) {
1387 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1388 return;
1389 }
1390 DCHECK(rl_src2.is_const);
1391 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1392 uint32_t val_lo = Low32Bits(val);
1393 uint32_t val_hi = High32Bits(val);
1394 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1395 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1396
1397 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001398 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 case Instruction::ADD_LONG:
1400 case Instruction::ADD_LONG_2ADDR:
1401 case Instruction::SUB_LONG:
1402 case Instruction::SUB_LONG_2ADDR:
1403 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1404 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1405 return;
1406 }
1407 break;
1408 default:
1409 break;
1410 }
1411 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1412 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1413 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1414 switch (opcode) {
1415 case Instruction::ADD_LONG:
1416 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001417 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001418 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 break;
1420 case Instruction::OR_LONG:
1421 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001422 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1423 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001425 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001426 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 }
1428 break;
1429 case Instruction::XOR_LONG:
1430 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001431 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1432 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 break;
1434 case Instruction::AND_LONG:
1435 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001436 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1437 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001439 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001440 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 }
1442 break;
1443 case Instruction::SUB_LONG_2ADDR:
1444 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001445 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001446 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 break;
1448 default:
1449 LOG(FATAL) << "Unexpected opcode " << opcode;
1450 }
1451 StoreValueWide(rl_dest, rl_result);
1452}
1453
1454} // namespace art