blob: 1c563bb1261bfa4975d25bdb2a29f5c021c4ad2f [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
Dave Allison3da67a52014-04-02 17:03:45 -070070void ArmMir2Lir::OpEndIT(LIR* it) {
71 // TODO: use the 'it' pointer to do some checks with the LIR, for example
72 // we could check that the number of instructions matches the mask
73 // in the IT instruction.
74 CHECK(it != nullptr);
75 GenBarrier();
76}
77
Brian Carlstrom7940e442013-07-12 13:46:57 -070078/*
79 * 64-bit 3way compare function.
80 * mov rX, #-1
81 * cmp op1hi, op2hi
82 * blt done
83 * bgt flip
84 * sub rX, op1lo, op2lo (treat as unsigned)
85 * beq done
86 * ite hi
87 * mov(hi) rX, #-1
88 * mov(!hi) rX, #1
89 * flip:
90 * neg rX
91 * done:
92 */
93void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070094 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 LIR* target1;
96 LIR* target2;
97 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
98 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080099 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800101 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 LIR* branch1 = OpCondBranch(kCondLt, NULL);
103 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800104 OpRegRegReg(kOpSub, t_reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 LIR* branch3 = OpCondBranch(kCondEq, NULL);
106
Dave Allison3da67a52014-04-02 17:03:45 -0700107 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800108 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700110 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
112 target2 = NewLIR0(kPseudoTargetLabel);
113 OpRegReg(kOpNeg, t_reg, t_reg);
114
115 target1 = NewLIR0(kPseudoTargetLabel);
116
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700117 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800118 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 StoreValue(rl_dest, rl_temp);
120 FreeTemp(t_reg);
121
122 branch1->target = target1;
123 branch2->target = target2;
124 branch3->target = branch1->target;
125}
126
127void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 int32_t val_lo = Low32Bits(val);
130 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700131 DCHECK_GE(ModifiedImmediate(val_lo), 0);
132 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700133 LIR* taken = &block_label_list_[bb->taken];
134 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800136 RegStorage low_reg = rl_src1.reg.GetLow();
137 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138
Vladimir Marko58af1f92013-12-19 13:31:15 +0000139 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800140 RegStorage t_reg = AllocTemp();
141 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000142 FreeTemp(t_reg);
143 OpCondBranch(ccode, taken);
144 return;
145 }
146
Brian Carlstromdf629502013-07-17 22:39:56 -0700147 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 case kCondEq:
149 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000150 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 break;
152 case kCondLt:
153 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
154 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000155 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 break;
157 case kCondLe:
158 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
159 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
160 ccode = kCondLs;
161 break;
162 case kCondGt:
163 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
164 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
165 ccode = kCondHi;
166 break;
167 case kCondGe:
168 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
169 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000170 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 break;
172 default:
173 LOG(FATAL) << "Unexpected ccode: " << ccode;
174 }
175 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
176}
177
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegLocation rl_result;
180 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 RegLocation rl_dest = mir_graph_->GetDest(mir);
182 rl_src = LoadValue(rl_src, kCoreReg);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000183 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 if (mir->ssa_rep->num_uses == 1) {
185 // CONST case
186 int true_val = mir->dalvikInsn.vB;
187 int false_val = mir->dalvikInsn.vC;
188 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000189 // Change kCondNe to kCondEq for the special cases below.
190 if (ccode == kCondNe) {
191 ccode = kCondEq;
192 std::swap(true_val, false_val);
193 }
194 bool cheap_false_val = InexpensiveConstantInt(false_val);
195 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800196 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000197 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700198 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800199 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700200 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000201 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800202 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000203 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700204 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800205 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700206 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000207 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800208 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700209 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800210 LoadConstant(rl_result.reg, true_val);
211 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700212 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 } else {
214 // Unlikely case - could be tuned.
buzbee2700f7e2014-03-07 09:46:20 -0800215 RegStorage t_reg1 = AllocTemp();
216 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 LoadConstant(t_reg1, true_val);
218 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800219 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700220 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800221 OpRegCopy(rl_result.reg, t_reg1);
222 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700223 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 }
225 } else {
226 // MOVE case
227 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
228 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
229 rl_true = LoadValue(rl_true, kCoreReg);
230 rl_false = LoadValue(rl_false, kCoreReg);
231 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800232 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700233 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000234 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700235 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800236 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000237 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700238 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800239 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700240 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700241 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800242 OpRegCopy(rl_result.reg, rl_true.reg);
243 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700244 }
Dave Allison3da67a52014-04-02 17:03:45 -0700245 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247 StoreValue(rl_dest, rl_result);
248}
249
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
252 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
253 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000254 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000256 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 ccode = FlipComparisonOrder(ccode);
258 }
259 if (rl_src2.is_const) {
260 RegLocation rl_temp = UpdateLocWide(rl_src2);
261 // Do special compare/branch against simple const operand if not already in registers.
262 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
263 if ((rl_temp.location != kLocPhysReg) &&
264 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
265 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
266 return;
267 }
268 }
buzbee0d829482013-10-11 15:24:55 -0700269 LIR* taken = &block_label_list_[bb->taken];
270 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
272 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800273 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700274 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 case kCondEq:
276 OpCondBranch(kCondNe, not_taken);
277 break;
278 case kCondNe:
279 OpCondBranch(kCondNe, taken);
280 break;
281 case kCondLt:
282 OpCondBranch(kCondLt, taken);
283 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000284 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 break;
286 case kCondLe:
287 OpCondBranch(kCondLt, taken);
288 OpCondBranch(kCondGt, not_taken);
289 ccode = kCondLs;
290 break;
291 case kCondGt:
292 OpCondBranch(kCondGt, taken);
293 OpCondBranch(kCondLt, not_taken);
294 ccode = kCondHi;
295 break;
296 case kCondGe:
297 OpCondBranch(kCondGt, taken);
298 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000299 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 break;
301 default:
302 LOG(FATAL) << "Unexpected ccode: " << ccode;
303 }
buzbee2700f7e2014-03-07 09:46:20 -0800304 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 OpCondBranch(ccode, taken);
306}
307
308/*
309 * Generate a register comparison to an immediate and branch. Caller
310 * is responsible for setting branch target field.
311 */
buzbee2700f7e2014-03-07 09:46:20 -0800312LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700315 /*
316 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
317 * compare-and-branch if zero is ideal if it will reach. However, because null checks
318 * branch forward to a launch pad, they will frequently not reach - and thus have to
319 * be converted to a long form during assembly (which will trigger another assembly
320 * pass). Here we estimate the branch distance for checks, and if large directly
321 * generate the long form in an attempt to avoid an extra assembly pass.
322 * TODO: consider interspersing launchpads in code following unconditional branches.
323 */
324 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
325 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
buzbee2700f7e2014-03-07 09:46:20 -0800326 if (!skip && (ARM_LOWREG(reg.GetReg())) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
328 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
buzbee2700f7e2014-03-07 09:46:20 -0800329 reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000331 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 branch = NewLIR2(kThumbBCond, 0, arm_cond);
333 }
334 branch->target = target;
335 return branch;
336}
337
buzbee2700f7e2014-03-07 09:46:20 -0800338LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 LIR* res;
340 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800341 // If src or dest is a pair, we'll be using low reg.
342 if (r_dest.IsPair()) {
343 r_dest = r_dest.GetLow();
344 }
345 if (r_src.IsPair()) {
346 r_src = r_src.GetLow();
347 }
348 if (ARM_FPREG(r_dest.GetReg()) || ARM_FPREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 return OpFpRegCopy(r_dest, r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800350 if (ARM_LOWREG(r_dest.GetReg()) && ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 opcode = kThumbMovRR;
buzbee2700f7e2014-03-07 09:46:20 -0800352 else if (!ARM_LOWREG(r_dest.GetReg()) && !ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 opcode = kThumbMovRR_H2H;
buzbee2700f7e2014-03-07 09:46:20 -0800354 else if (ARM_LOWREG(r_dest.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 opcode = kThumbMovRR_H2L;
356 else
357 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800358 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
360 res->flags.is_nop = true;
361 }
362 return res;
363}
364
buzbee2700f7e2014-03-07 09:46:20 -0800365LIR* ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
367 AppendLIR(res);
368 return res;
369}
370
buzbee2700f7e2014-03-07 09:46:20 -0800371void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
372 bool dest_fp = ARM_FPREG(r_dest.GetLowReg());
373 bool src_fp = ARM_FPREG(r_src.GetLowReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 if (dest_fp) {
375 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 // FIXME: handle 64-bit solo's here.
377 OpRegCopy(RegStorage::Solo64(S2d(r_dest.GetLowReg(), r_dest.GetHighReg())),
378 RegStorage::Solo64(S2d(r_src.GetLowReg(), r_src.GetHighReg())));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800380 NewLIR3(kThumb2Fmdrr, S2d(r_dest.GetLowReg(), r_dest.GetHighReg()),
381 r_src.GetLowReg(), r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383 } else {
384 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800385 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), S2d(r_src.GetLowReg(),
386 r_src.GetHighReg()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 } else {
388 // Handle overlap
buzbee2700f7e2014-03-07 09:46:20 -0800389 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
390 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
391 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
392 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800394 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
395 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
397 }
398 }
399}
400
401// Table of magic divisors
402struct MagicTable {
403 uint32_t magic;
404 uint32_t shift;
405 DividePattern pattern;
406};
407
408static const MagicTable magic_table[] = {
409 {0, 0, DivideNone}, // 0
410 {0, 0, DivideNone}, // 1
411 {0, 0, DivideNone}, // 2
412 {0x55555556, 0, Divide3}, // 3
413 {0, 0, DivideNone}, // 4
414 {0x66666667, 1, Divide5}, // 5
415 {0x2AAAAAAB, 0, Divide3}, // 6
416 {0x92492493, 2, Divide7}, // 7
417 {0, 0, DivideNone}, // 8
418 {0x38E38E39, 1, Divide5}, // 9
419 {0x66666667, 2, Divide5}, // 10
420 {0x2E8BA2E9, 1, Divide5}, // 11
421 {0x2AAAAAAB, 1, Divide5}, // 12
422 {0x4EC4EC4F, 2, Divide5}, // 13
423 {0x92492493, 3, Divide7}, // 14
424 {0x88888889, 3, Divide7}, // 15
425};
426
427// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700428bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700429 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
431 return false;
432 }
433 DividePattern pattern = magic_table[lit].pattern;
434 if (pattern == DivideNone) {
435 return false;
436 }
Brian Carlstrom3654a6f2014-03-27 17:14:26 -0700437 // Tuning: add rem patterns
438 if (!is_div) {
439 return false;
440 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441
buzbee2700f7e2014-03-07 09:46:20 -0800442 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 LoadConstant(r_magic, magic_table[lit].magic);
444 rl_src = LoadValue(rl_src, kCoreReg);
445 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800446 RegStorage r_hi = AllocTemp();
447 RegStorage r_lo = AllocTemp();
448 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700449 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 case Divide3:
Ian Rogerse2143c02014-03-28 08:47:16 -0700451 OpRegRegRegShift(kOpSub, rl_result.reg, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 break;
453 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800454 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Ian Rogerse2143c02014-03-28 08:47:16 -0700455 OpRegRegRegShift(kOpRsub, rl_result.reg, r_lo, r_hi,
456 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 break;
458 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800459 OpRegReg(kOpAdd, r_hi, rl_src.reg);
460 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Ian Rogerse2143c02014-03-28 08:47:16 -0700461 OpRegRegRegShift(kOpRsub, rl_result.reg, r_lo, r_hi,
462 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 break;
464 default:
465 LOG(FATAL) << "Unexpected pattern: " << pattern;
466 }
467 StoreValue(rl_dest, rl_result);
468 return true;
469}
470
Ian Rogerse2143c02014-03-28 08:47:16 -0700471// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
472bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
473 if (IsPowerOfTwo(lit)) {
474 op->op = kOpLsl;
475 op->shift = LowestSetBit(lit);
476 return true;
477 }
478
479 if (IsPowerOfTwo(lit - 1)) {
480 op->op = kOpAdd;
481 op->shift = LowestSetBit(lit - 1);
482 return true;
483 }
484
485 if (IsPowerOfTwo(lit + 1)) {
486 op->op = kOpRsub;
487 op->shift = LowestSetBit(lit + 1);
488 return true;
489 }
490
491 op->op = kOpInvalid;
492 return false;
493}
494
495// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
496bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
497 GetEasyMultiplyOp(lit, &ops[0]);
498 if (GetEasyMultiplyOp(lit, &ops[0])) {
499 ops[1].op = kOpInvalid;
500 return true;
501 }
502
503 int lit1 = lit;
504 uint32_t shift = LowestSetBit(lit1);
505 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
506 ops[1].op = kOpLsl;
507 ops[1].shift = shift;
508 return true;
509 }
510
511 lit1 = lit - 1;
512 shift = LowestSetBit(lit1);
513 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
514 ops[1].op = kOpAdd;
515 ops[1].shift = shift;
516 return true;
517 }
518
519 lit1 = lit + 1;
520 shift = LowestSetBit(lit1);
521 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
522 ops[1].op = kOpRsub;
523 ops[1].shift = shift;
524 return true;
525 }
526
527 return false;
528}
529
530void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
531 // dest = ( src << shift1) + [ src | -src | 0 ]
532 // dest = (dest << shift2) + [ src | -src | 0 ]
533 for (int i = 0; i < 2; i++) {
534 RegStorage r_src2;
535 if (i == 0) {
536 r_src2 = r_src;
537 } else {
538 r_src2 = r_dest;
539 }
540 switch (ops[i].op) {
541 case kOpLsl:
542 OpRegRegImm(kOpLsl, r_dest, r_src2, ops[i].shift);
543 break;
544 case kOpAdd:
545 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_src2, EncodeShift(kArmLsl, ops[i].shift));
546 break;
547 case kOpRsub:
548 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_src2, EncodeShift(kArmLsl, ops[i].shift));
549 break;
550 default:
551 DCHECK_NE(i, 0);
552 DCHECK_EQ(ops[i].op, kOpInvalid);
553 break;
554 }
555 }
556}
557
558bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
559 EasyMultiplyOp ops[2];
560
561 if (!GetEasyMultiplyTwoOps(lit, ops)) {
562 return false;
563 }
564
565 rl_src = LoadValue(rl_src, kCoreReg);
566 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
567
568 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
569 StoreValue(rl_dest, rl_result);
570 return true;
571}
572
buzbee2700f7e2014-03-07 09:46:20 -0800573LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code, RegStorage reg1, RegStorage base,
574 int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
576 return NULL;
577}
578
Mark Mendell2bf31e62014-01-23 12:13:40 -0800579RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
580 RegLocation rl_src2, bool is_div, bool check_zero) {
581 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
582 return rl_dest;
583}
584
585RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
586 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
587 return rl_dest;
588}
589
buzbee2700f7e2014-03-07 09:46:20 -0800590RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700591 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
592
593 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800594 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700595 LoadConstant(lit_temp, lit);
596 // Use the generic case for div/rem with arg2 in a register.
597 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
598 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
599 FreeTemp(lit_temp);
600
601 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602}
603
buzbee2700f7e2014-03-07 09:46:20 -0800604RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700605 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700606 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
607 if (is_div) {
608 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800609 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700610 } else {
611 // Remainder case, use the following code:
612 // temp = reg1 / reg2 - integer division
613 // temp = temp * reg2
614 // dest = reg1 - temp
615
buzbee2700f7e2014-03-07 09:46:20 -0800616 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700617 OpRegRegReg(kOpDiv, temp, reg1, reg2);
618 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800619 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700620 FreeTemp(temp);
621 }
622
623 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624}
625
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700626bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 DCHECK_EQ(cu_->instruction_set, kThumb2);
628 RegLocation rl_src1 = info->args[0];
629 RegLocation rl_src2 = info->args[1];
630 rl_src1 = LoadValue(rl_src1, kCoreReg);
631 rl_src2 = LoadValue(rl_src2, kCoreReg);
632 RegLocation rl_dest = InlineTarget(info);
633 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800634 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700635 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800636 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
637 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700638 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 StoreValue(rl_dest, rl_result);
640 return true;
641}
642
Vladimir Markoe508a202013-11-04 15:24:22 +0000643bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
644 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800645 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000646 RegLocation rl_dest = InlineTarget(info);
647 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
648 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
649 if (size == kLong) {
650 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800651 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
652 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
653 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000654 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800655 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
656 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000657 }
658 StoreValueWide(rl_dest, rl_result);
659 } else {
660 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
661 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800662 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000663 StoreValue(rl_dest, rl_result);
664 }
665 return true;
666}
667
668bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
669 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800670 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000671 RegLocation rl_src_value = info->args[2]; // [size] value
672 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
673 if (size == kLong) {
674 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
675 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800676 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), kWord);
677 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000678 } else {
679 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
680 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
681 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800682 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000683 }
684 return true;
685}
686
buzbee2700f7e2014-03-07 09:46:20 -0800687void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 LOG(FATAL) << "Unexpected use of OpLea for Arm";
689}
690
Ian Rogersdd7624d2014-03-14 17:43:00 -0700691void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
693}
694
Vladimir Marko1c282e22013-11-21 14:49:47 +0000695bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 DCHECK_EQ(cu_->instruction_set, kThumb2);
697 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000698 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
699 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800700 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000701 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000702 // If is_long, high half is in info->args[5]
703 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
704 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 RegLocation rl_dest = InlineTarget(info); // boolean place for result
706
Vladimir Marko3e5af822013-11-21 15:01:20 +0000707 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
708 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
709 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
710 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
711 // into the same temps, reducing the number of required temps down to 5. We shall work
712 // around the potentially locked temp by using LR for r_ptr, unconditionally.
713 // TODO: Pass information about the need for more temps to the stack frame generation
714 // code so that we can rely on being able to allocate enough temps.
715 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
716 MarkTemp(rARM_LR);
717 FreeTemp(rARM_LR);
718 LockTemp(rARM_LR);
719 bool load_early = true;
720 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800721 int expected_reg = is_long ? rl_src_expected.reg.GetLowReg() : rl_src_expected.reg.GetReg();
722 int new_val_reg = is_long ? rl_src_new_value.reg.GetLowReg() : rl_src_new_value.reg.GetReg();
723 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !IsFpReg(expected_reg);
724 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !IsFpReg(new_val_reg);
725 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
726 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000727
728 if (!expected_is_good_reg && !new_value_is_good_reg) {
729 // None of expected/new_value is non-temp reg, need to load both late
730 load_early = false;
731 // Make sure they are not in the temp regs and the load will not be skipped.
732 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800733 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000734 ClobberSReg(rl_src_expected.s_reg_low);
735 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
736 rl_src_expected.location = kLocDalvikFrame;
737 }
738 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800739 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000740 ClobberSReg(rl_src_new_value.s_reg_low);
741 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
742 rl_src_new_value.location = kLocDalvikFrame;
743 }
744 }
745 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746
747 // Release store semantics, get the barrier out of the way. TODO: revisit
748 GenMemBarrier(kStoreLoad);
749
750 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000751 RegLocation rl_new_value;
752 if (!is_long) {
753 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
754 } else if (load_early) {
755 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
756 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757
Vladimir Marko1c282e22013-11-21 14:49:47 +0000758 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800760 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 }
762
763 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
764
buzbee2700f7e2014-03-07 09:46:20 -0800765 RegStorage r_ptr = rs_rARM_LR;
766 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767
768 // Free now unneeded rl_object and rl_offset to give more temps.
769 ClobberSReg(rl_object.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000770 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000772 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773
Vladimir Marko3e5af822013-11-21 15:01:20 +0000774 RegLocation rl_expected;
775 if (!is_long) {
776 rl_expected = LoadValue(rl_src_expected, kCoreReg);
777 } else if (load_early) {
778 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
779 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000780 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee2700f7e2014-03-07 09:46:20 -0800781 int low_reg = AllocTemp().GetReg();
782 int high_reg = AllocTemp().GetReg();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000783 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
784 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000785 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786
Vladimir Marko3e5af822013-11-21 15:01:20 +0000787 // do {
788 // tmp = [r_ptr] - expected;
789 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
790 // result = tmp != 0;
791
buzbee2700f7e2014-03-07 09:46:20 -0800792 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700793 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700794
Dave Allison3da67a52014-04-02 17:03:45 -0700795 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000796 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800797 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000798 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800799 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000800 }
buzbee2700f7e2014-03-07 09:46:20 -0800801 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
802 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
803 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000804 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800805 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000806 }
807 // Make sure we use ORR that sets the ccode
buzbee2700f7e2014-03-07 09:46:20 -0800808 if (ARM_LOWREG(r_tmp.GetReg()) && ARM_LOWREG(r_tmp_high.GetReg())) {
809 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000810 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800811 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000812 }
813 FreeTemp(r_tmp_high); // Now unneeded
814
815 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700816 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800817 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 +0000818
819 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800820 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
821 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000822 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700823 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800824 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000825 }
826
827 // Still one conditional left from OpIT(kCondEq, "T") from either branch
828 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700829 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700830
Jeff Hao2de2aa12013-09-12 17:20:31 -0700831 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832
Vladimir Marko3e5af822013-11-21 15:01:20 +0000833 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800834 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000835 }
836
837 // result := (tmp1 != 0) ? 0 : 1;
838 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800839 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000840 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700841 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800842 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000843 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700844 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000845
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 StoreValue(rl_dest, rl_result);
847
Vladimir Marko3e5af822013-11-21 15:01:20 +0000848 // Now, restore lr to its non-temp status.
849 Clobber(rARM_LR);
850 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 return true;
852}
853
buzbee2700f7e2014-03-07 09:46:20 -0800854LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
855 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856}
857
buzbee2700f7e2014-03-07 09:46:20 -0800858LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
859 return NewLIR3(kThumb2Vldms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860}
861
buzbee2700f7e2014-03-07 09:46:20 -0800862LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
863 return NewLIR3(kThumb2Vstms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864}
865
866void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
867 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700868 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700869 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 EncodeShift(kArmLsl, second_bit - first_bit));
871 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800872 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 }
874}
875
buzbee2700f7e2014-03-07 09:46:20 -0800876void ArmMir2Lir::GenDivZeroCheck(RegStorage reg) {
877 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
878 RegStorage t_reg = AllocTemp();
879 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 FreeTemp(t_reg);
881 GenCheck(kCondEq, kThrowDivZero);
882}
883
884// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700885LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
887 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
888}
889
890// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800891LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000893 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
894 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 return OpCondBranch(c_code, target);
896}
897
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700898void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800900 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
901 LIR* barrier = last_lir_insn_;
902
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 int dmb_flavor;
904 // TODO: revisit Arm barrier kinds
905 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800906 case kLoadStore: dmb_flavor = kISH; break;
907 case kLoadLoad: dmb_flavor = kISH; break;
908 case kStoreStore: dmb_flavor = kISHST; break;
909 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 default:
911 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
912 dmb_flavor = kSY; // quiet gcc.
913 break;
914 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800915
916 // If the same barrier already exists, don't generate another.
917 if (barrier == nullptr
918 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
919 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
920 }
921
922 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
923 DCHECK(!barrier->flags.use_def_invalid);
924 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925#endif
926}
927
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700928void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700929 rl_src = LoadValueWide(rl_src, kCoreReg);
930 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800931 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 LoadConstantNoClobber(z_reg, 0);
933 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -0800934 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
935 RegStorage t_reg = AllocTemp();
936 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
937 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938 FreeTemp(t_reg);
939 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800940 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
941 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 }
943 FreeTemp(z_reg);
944 StoreValueWide(rl_dest, rl_result);
945}
946
Mark Mendelle02d48f2014-01-15 11:19:23 -0800947void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
948 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 /*
Zheng Xud7f8e022014-03-13 13:40:30 +0000950 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
951 * dest = src1.lo * src2.lo;
952 * tmp1 += src1.lo * src2.hi;
953 * dest.hi += tmp1;
954 *
955 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 * registers. Normally for Arm, we get 5. We can get to 6 by including
957 * lr in the temp set. The only problematic case is all operands and result are
958 * distinct, and none have been promoted. In that case, we can succeed by aggressively
959 * freeing operand temp registers after they are no longer needed. All other cases
960 * can proceed normally. We'll just punt on the case of the result having a misaligned
961 * overlap with either operand and send that case to a runtime handler.
962 */
963 RegLocation rl_result;
964 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700965 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 FlushAllRegs();
967 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
968 rl_result = GetReturnWide(false);
969 StoreValueWide(rl_dest, rl_result);
970 return;
971 }
Zheng Xud7f8e022014-03-13 13:40:30 +0000972
973 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
974 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
975
976 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -0800977 RegStorage res_lo;
978 RegStorage res_hi;
979 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
980 !IsTemp(rl_dest.reg.GetLowReg()) && !IsTemp(rl_dest.reg.GetHighReg());
981 bool src1_promoted = !IsTemp(rl_src1.reg.GetLowReg()) && !IsTemp(rl_src1.reg.GetHighReg());
982 bool src2_promoted = !IsTemp(rl_src2.reg.GetLowReg()) && !IsTemp(rl_src2.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +0000983 // Check if rl_dest is *not* either operand and we have enough temp registers.
984 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
985 (dest_promoted || src1_promoted || src2_promoted)) {
986 // In this case, we do not need to manually allocate temp registers for result.
987 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800988 res_lo = rl_result.reg.GetLow();
989 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +0000990 } else {
991 res_lo = AllocTemp();
992 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
993 // In this case, we have enough temp registers to be allocated for result.
994 res_hi = AllocTemp();
995 reg_status = 1;
996 } else {
997 // In this case, all temps are now allocated.
998 // res_hi will be allocated after we can free src1_hi.
999 reg_status = 2;
1000 }
1001 }
1002
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 // Temporarily add LR to the temp pool, and assign it to tmp1
1004 MarkTemp(rARM_LR);
1005 FreeTemp(rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001006 RegStorage tmp1 = rs_rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 LockTemp(rARM_LR);
1008
buzbee2700f7e2014-03-07 09:46:20 -08001009 if (rl_src1.reg == rl_src2.reg) {
1010 DCHECK(res_hi.Valid());
1011 DCHECK(res_lo.Valid());
1012 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1013 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1014 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001015 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001017 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001018 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001019 DCHECK(!res_hi.Valid());
1020 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001021 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1022 FreeTemp(rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001023 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
buzbee2700f7e2014-03-07 09:46:20 -08001025 DCHECK(res_hi.Valid());
1026 DCHECK(res_lo.Valid());
1027 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1028 rl_src1.reg.GetLowReg());
1029 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1030 tmp1.GetReg());
1031 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001032 if (reg_status == 2) {
1033 // Clobber rl_src1 since it was corrupted.
buzbee2700f7e2014-03-07 09:46:20 -08001034 FreeTemp(rl_src1.reg);
1035 Clobber(rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 }
1037 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001038
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001040 FreeTemp(tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 Clobber(rARM_LR);
1042 UnmarkTemp(rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001043
1044 if (reg_status != 0) {
1045 // We had manually allocated registers for rl_result.
1046 // Now construct a RegLocation.
1047 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001048 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001049 }
1050
1051 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052}
1053
Mark Mendelle02d48f2014-01-15 11:19:23 -08001054void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001055 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1057}
1058
Mark Mendelle02d48f2014-01-15 11:19:23 -08001059void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001060 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1062}
1063
Mark Mendelle02d48f2014-01-15 11:19:23 -08001064void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001065 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1067}
1068
Mark Mendelle02d48f2014-01-15 11:19:23 -08001069void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001070 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1072}
1073
Mark Mendelle02d48f2014-01-15 11:19:23 -08001074void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001075 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1077}
1078
1079/*
1080 * Generate array load
1081 */
1082void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001083 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 RegisterClass reg_class = oat_reg_class_by_size(size);
1085 int len_offset = mirror::Array::LengthOffset().Int32Value();
1086 int data_offset;
1087 RegLocation rl_result;
1088 bool constant_index = rl_index.is_const;
1089 rl_array = LoadValue(rl_array, kCoreReg);
1090 if (!constant_index) {
1091 rl_index = LoadValue(rl_index, kCoreReg);
1092 }
1093
1094 if (rl_dest.wide) {
1095 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1096 } else {
1097 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1098 }
1099
1100 // If index is constant, just fold it into the data offset
1101 if (constant_index) {
1102 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1103 }
1104
1105 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001106 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107
1108 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001109 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 if (needs_range_check) {
1111 reg_len = AllocTemp();
1112 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001113 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001114 MarkPossibleNullPointerException(opt_flags);
1115 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001116 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 }
1118 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001119 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001121 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
1123 // No special indexed operation, lea + load w/ displacement
1124 reg_ptr = AllocTemp();
Ian Rogerse2143c02014-03-28 08:47:16 -07001125 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001126 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 }
1128 rl_result = EvalLoc(rl_dest, reg_class, true);
1129
1130 if (needs_range_check) {
1131 if (constant_index) {
1132 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1133 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001134 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 }
1136 FreeTemp(reg_len);
1137 }
1138 if (rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001139 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001140 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 if (!constant_index) {
1142 FreeTemp(reg_ptr);
1143 }
1144 StoreValueWide(rl_dest, rl_result);
1145 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001146 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001147 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 if (!constant_index) {
1149 FreeTemp(reg_ptr);
1150 }
1151 StoreValue(rl_dest, rl_result);
1152 }
1153 } else {
1154 // Offset base, then use indexed load
buzbee2700f7e2014-03-07 09:46:20 -08001155 RegStorage reg_ptr = AllocTemp();
1156 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001157 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 rl_result = EvalLoc(rl_dest, reg_class, true);
1159
1160 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001161 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 FreeTemp(reg_len);
1163 }
buzbee2700f7e2014-03-07 09:46:20 -08001164 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001165 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 FreeTemp(reg_ptr);
1167 StoreValue(rl_dest, rl_result);
1168 }
1169}
1170
1171/*
1172 * Generate array store
1173 *
1174 */
1175void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001176 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 RegisterClass reg_class = oat_reg_class_by_size(size);
1178 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 bool constant_index = rl_index.is_const;
1180
Ian Rogersa9a82542013-10-04 11:17:26 -07001181 int data_offset;
1182 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1184 } else {
1185 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1186 }
1187
1188 // If index is constant, just fold it into the data offset.
1189 if (constant_index) {
1190 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1191 }
1192
1193 rl_array = LoadValue(rl_array, kCoreReg);
1194 if (!constant_index) {
1195 rl_index = LoadValue(rl_index, kCoreReg);
1196 }
1197
buzbee2700f7e2014-03-07 09:46:20 -08001198 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001199 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001201 reg_ptr = rl_array.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001202 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1203 Clobber(rl_array.reg.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001204 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001206 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 reg_ptr = AllocTemp();
1208 }
1209
1210 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001211 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212
1213 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001214 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 if (needs_range_check) {
1216 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001217 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001219 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001220 MarkPossibleNullPointerException(opt_flags);
1221 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001222 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 }
1224 /* at this point, reg_ptr points to array, 2 live temps */
1225 if (rl_src.wide || rl_src.fp || constant_index) {
1226 if (rl_src.wide) {
1227 rl_src = LoadValueWide(rl_src, reg_class);
1228 } else {
1229 rl_src = LoadValue(rl_src, reg_class);
1230 }
1231 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001232 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 }
1234 if (needs_range_check) {
1235 if (constant_index) {
1236 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1237 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001238 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240 FreeTemp(reg_len);
1241 }
1242
1243 if (rl_src.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001244 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001246 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 }
Dave Allisonb373e092014-02-20 16:06:36 -08001248 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 } else {
1250 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001251 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 rl_src = LoadValue(rl_src, reg_class);
1253 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001254 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 FreeTemp(reg_len);
1256 }
buzbee2700f7e2014-03-07 09:46:20 -08001257 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001258 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
Ian Rogers773aab12013-10-14 13:50:10 -07001260 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 FreeTemp(reg_ptr);
1262 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001263 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001264 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 }
1266}
1267
Ian Rogersa9a82542013-10-04 11:17:26 -07001268
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001270 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 rl_src = LoadValueWide(rl_src, kCoreReg);
1272 // Per spec, we only care about low 6 bits of shift amount.
1273 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1274 if (shift_amount == 0) {
1275 StoreValueWide(rl_dest, rl_src);
1276 return;
1277 }
1278 if (BadOverlap(rl_src, rl_dest)) {
1279 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1280 return;
1281 }
1282 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001283 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 case Instruction::SHL_LONG:
1285 case Instruction::SHL_LONG_2ADDR:
1286 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001287 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1288 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001290 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1291 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001293 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1294 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001296 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001297 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001299 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
1301 break;
1302 case Instruction::SHR_LONG:
1303 case Instruction::SHR_LONG_2ADDR:
1304 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001305 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1306 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001308 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1309 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001311 RegStorage t_reg = AllocTemp();
1312 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001313 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 EncodeShift(kArmLsl, 32 - shift_amount));
1315 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001316 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 }
1318 break;
1319 case Instruction::USHR_LONG:
1320 case Instruction::USHR_LONG_2ADDR:
1321 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001322 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1323 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001325 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1326 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001328 RegStorage t_reg = AllocTemp();
1329 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001330 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 EncodeShift(kArmLsl, 32 - shift_amount));
1332 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001333 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 }
1335 break;
1336 default:
1337 LOG(FATAL) << "Unexpected case";
1338 }
1339 StoreValueWide(rl_dest, rl_result);
1340}
1341
1342void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001343 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1345 if (!rl_src2.is_const) {
1346 // Don't bother with special handling for subtract from immediate.
1347 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1348 return;
1349 }
1350 } else {
1351 // Normalize
1352 if (!rl_src2.is_const) {
1353 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001354 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356 }
1357 if (BadOverlap(rl_src1, rl_dest)) {
1358 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1359 return;
1360 }
1361 DCHECK(rl_src2.is_const);
1362 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1363 uint32_t val_lo = Low32Bits(val);
1364 uint32_t val_hi = High32Bits(val);
1365 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1366 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1367
1368 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001369 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 case Instruction::ADD_LONG:
1371 case Instruction::ADD_LONG_2ADDR:
1372 case Instruction::SUB_LONG:
1373 case Instruction::SUB_LONG_2ADDR:
1374 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1375 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1376 return;
1377 }
1378 break;
1379 default:
1380 break;
1381 }
1382 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1383 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1384 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1385 switch (opcode) {
1386 case Instruction::ADD_LONG:
1387 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001388 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001389 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 break;
1391 case Instruction::OR_LONG:
1392 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001393 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1394 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001396 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001397 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 }
1399 break;
1400 case Instruction::XOR_LONG:
1401 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001402 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1403 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 break;
1405 case Instruction::AND_LONG:
1406 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001407 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1408 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001410 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001411 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 }
1413 break;
1414 case Instruction::SUB_LONG_2ADDR:
1415 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001416 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001417 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418 break;
1419 default:
1420 LOG(FATAL) << "Unexpected opcode " << opcode;
1421 }
1422 StoreValueWide(rl_dest, rl_result);
1423}
1424
1425} // namespace art