1efc6369224b036a1fb77849f7ae65b3492c832c0buzbee/*
2efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * Copyright (C) 2011 The Android Open Source Project
3efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *
4efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * Licensed under the Apache License, Version 2.0 (the "License");
5efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * you may not use this file except in compliance with the License.
6efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * You may obtain a copy of the License at
7efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *
8efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *      http://www.apache.org/licenses/LICENSE-2.0
9efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *
10efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * Unless required by applicable law or agreed to in writing, software
11efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * distributed under the License is distributed on an "AS IS" BASIS,
12efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * See the License for the specific language governing permissions and
14efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * limitations under the License.
15efc6369224b036a1fb77849f7ae65b3492c832c0buzbee */
16efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
17efc6369224b036a1fb77849f7ae65b3492c832c0buzbee/* This file contains codegen for the Thumb2 ISA. */
18efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
191bc37c60da71c923ea9a2e99d31ba1b3d76d79a8buzbee#include "arm_lir.h"
2002031b185b4653e6c72e21f7a51238b903f6d638buzbee#include "codegen_arm.h"
217940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "dex/quick/mir_to_lir-inl.h"
22b5860fb459f1ed71f39d8a87b45bee6727d79fe8buzbee#include "dex/reg_storage_eq.h"
23166db04e259ca51838c311891598664deeed85adIan Rogers#include "entrypoints/quick/quick_entrypoints.h"
2433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers#include "mirror/array.h"
25efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
26efc6369224b036a1fb77849f7ae65b3492c832c0buzbeenamespace art {
27efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
291fd3346740dfb7f47be9922312b68a4227fada96buzbee  OpRegReg(kOpCmp, src1, src2);
301fd3346740dfb7f47be9922312b68a4227fada96buzbee  return OpCondBranch(cond, target);
31efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
32efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
33efc6369224b036a1fb77849f7ae65b3492c832c0buzbee/*
34efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * Generate a Thumb2 IT instruction, which can nullify up to
35efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * four subsequent instructions based on a condition and its
36efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * inverse.  The condition applies to the first instruction, which
37efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * is executed if the condition is met.  The string "guide" consists
38efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
39efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * A "T" means the instruction is executed if the condition is
40efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * met, and an "E" means the instruction is executed if the condition
41efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * is not met.
42efc6369224b036a1fb77849f7ae65b3492c832c0buzbee */
432ce745c06271d5223d57dbf08117b20d5b60694aBrian CarlstromLIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
44efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  int mask;
45efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  int mask3 = 0;
46efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  int mask2 = 0;
47efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  int mask1 = 0;
4802031b185b4653e6c72e21f7a51238b903f6d638buzbee  ArmConditionCode code = ArmConditionEncoding(ccode);
4902031b185b4653e6c72e21f7a51238b903f6d638buzbee  int cond_bit = code & 1;
5002031b185b4653e6c72e21f7a51238b903f6d638buzbee  int alt_bit = cond_bit ^ 1;
51efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
527934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  // Note: case fallthroughs intentional
53efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  switch (strlen(guide)) {
54efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case 3:
55fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee      mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
56efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case 2:
57fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee      mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
58efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case 1:
59fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee      mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
60efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
61efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case 0:
62efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
63efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    default:
6452a77fc135f0e0df57ee24641c3f5ae415ff7bd6buzbee      LOG(FATAL) << "OAT: bad case in OpIT";
65efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
66efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
67efc6369224b036a1fb77849f7ae65b3492c832c0buzbee       (1 << (3 - strlen(guide)));
681fd3346740dfb7f47be9922312b68a4227fada96buzbee  return NewLIR2(kThumb2It, code, mask);
69efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
70efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
71b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampevoid ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
72b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int mask;
73b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int mask3 = 0;
74b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int mask2 = 0;
75b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int mask1 = 0;
76b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
77b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int cond_bit = code & 1;
78b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  int alt_bit = cond_bit ^ 1;
79b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe
80b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  // Note: case fallthroughs intentional
81b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  switch (strlen(new_guide)) {
82b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    case 3:
83b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
84b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    case 2:
85b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
86b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    case 1:
87b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
88b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      break;
89b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    case 0:
90b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      break;
91b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    default:
92b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      LOG(FATAL) << "OAT: bad case in UpdateIT";
93b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  }
94b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
95b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe      (1 << (3 - strlen(new_guide)));
96b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  it->operands[1] = mask;
97b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe}
98b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe
993da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allisonvoid ArmMir2Lir::OpEndIT(LIR* it) {
1003da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  // TODO: use the 'it' pointer to do some checks with the LIR, for example
1013da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  //       we could check that the number of instructions matches the mask
1023da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  //       in the IT instruction.
1033da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  CHECK(it != nullptr);
1043da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  GenBarrier();
1053da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison}
1063da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison
107efc6369224b036a1fb77849f7ae65b3492c832c0buzbee/*
108efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * 64-bit 3way compare function.
109efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     mov   rX, #-1
110efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     cmp   op1hi, op2hi
111efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     blt   done
112efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     bgt   flip
113efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     sub   rX, op1lo, op2lo (treat as unsigned)
114efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     beq   done
115efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     ite   hi
116efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     mov(hi)   rX, #-1
117efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     mov(!hi)  rX, #1
118efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * flip:
119efc6369224b036a1fb77849f7ae65b3492c832c0buzbee *     neg   rX
120efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * done:
121efc6369224b036a1fb77849f7ae65b3492c832c0buzbee */
122a1983d4dab10b0cc51e9d1b6bcafa9a723fabcd9buzbeevoid ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
123efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  LIR* target1;
124efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  LIR* target2;
1251fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1261fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1272700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage t_reg = AllocTemp();
1281fd3346740dfb7f47be9922312b68a4227fada96buzbee  LoadConstant(t_reg, -1);
1292700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1301fd3346740dfb7f47be9922312b68a4227fada96buzbee  LIR* branch1 = OpCondBranch(kCondLt, NULL);
1311fd3346740dfb7f47be9922312b68a4227fada96buzbee  LIR* branch2 = OpCondBranch(kCondGt, NULL);
132a1983d4dab10b0cc51e9d1b6bcafa9a723fabcd9buzbee  OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1331fd3346740dfb7f47be9922312b68a4227fada96buzbee  LIR* branch3 = OpCondBranch(kCondEq, NULL);
1341fd3346740dfb7f47be9922312b68a4227fada96buzbee
1353da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  LIR* it = OpIT(kCondHi, "E");
1362700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
1371fd3346740dfb7f47be9922312b68a4227fada96buzbee  LoadConstant(t_reg, 1);
1383da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  OpEndIT(it);
1391fd3346740dfb7f47be9922312b68a4227fada96buzbee
1401fd3346740dfb7f47be9922312b68a4227fada96buzbee  target2 = NewLIR0(kPseudoTargetLabel);
1411fd3346740dfb7f47be9922312b68a4227fada96buzbee  OpRegReg(kOpNeg, t_reg, t_reg);
1421fd3346740dfb7f47be9922312b68a4227fada96buzbee
1431fd3346740dfb7f47be9922312b68a4227fada96buzbee  target1 = NewLIR0(kPseudoTargetLabel);
144fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee
1457934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  RegLocation rl_temp = LocCReturn();  // Just using as template, will change
1462700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  rl_temp.reg.SetReg(t_reg.GetReg());
1471fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValue(rl_dest, rl_temp);
1481fd3346740dfb7f47be9922312b68a4227fada96buzbee  FreeTemp(t_reg);
149efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
150cbd6d44c0a94f3d26671b5325aa21bbf1335ffe8buzbee  branch1->target = target1;
151cbd6d44c0a94f3d26671b5325aa21bbf1335ffe8buzbee  branch2->target = target2;
152efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  branch3->target = branch1->target;
153efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
154efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1551fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
1562ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                          int64_t val, ConditionCode ccode) {
1574ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  int32_t val_lo = Low32Bits(val);
1584ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  int32_t val_hi = High32Bits(val);
1594274889d48ef82369bf2c1ca70d84689b4f9e93aBrian Carlstrom  DCHECK_GE(ModifiedImmediate(val_lo), 0);
1604274889d48ef82369bf2c1ca70d84689b4f9e93aBrian Carlstrom  DCHECK_GE(ModifiedImmediate(val_hi), 0);
1610d82948094d9a198e01aa95f64012bdedd5b6fc9buzbee  LIR* taken = &block_label_list_[bb->taken];
1620d82948094d9a198e01aa95f64012bdedd5b6fc9buzbee  LIR* not_taken = &block_label_list_[bb->fall_through];
1631fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1642700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage low_reg = rl_src1.reg.GetLow();
1652700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage high_reg = rl_src1.reg.GetHigh();
1664ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
16758af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko  if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
1682700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage t_reg = AllocTemp();
1692700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
17058af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko    FreeTemp(t_reg);
17158af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko    OpCondBranch(ccode, taken);
17258af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko    return;
17358af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko  }
17458af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko
175df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  switch (ccode) {
1764ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondEq:
1774ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondNe:
17858af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
1794ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
1804ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondLt:
1811fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
1821fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
18358af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      ccode = kCondUlt;
1844ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
1854ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondLe:
1861fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
1871fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
1884ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      ccode = kCondLs;
1894ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
1904ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondGt:
1911fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
1921fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
1934ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      ccode = kCondHi;
1944ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
1954ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case kCondGe:
1961fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
1971fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
19858af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      ccode = kCondUge;
1994ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
2004ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    default:
2014ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      LOG(FATAL) << "Unexpected ccode: " << ccode;
2024ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
2031fd3346740dfb7f47be9922312b68a4227fada96buzbee  OpCmpImmBranch(ccode, low_reg, val_lo, taken);
2044ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee}
2054ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
20690969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampevoid ArmMir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
20790969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe                                  int32_t true_val, int32_t false_val, RegStorage rs_dest,
20890969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe                                  int dest_reg_class) {
20990969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  // TODO: Generalize the IT below to accept more than one-instruction loads.
21090969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  DCHECK(InexpensiveConstantInt(true_val));
21190969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  DCHECK(InexpensiveConstantInt(false_val));
21290969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe
21390969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  if ((true_val == 0 && code == kCondEq) ||
21490969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe      (false_val == 0 && code == kCondNe)) {
21590969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
21690969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
21790969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    LIR* it = OpIT(kCondNe, "");
21890969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
21990969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    OpEndIT(it);
22090969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe    return;
22190969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  }
22290969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe
22390969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  OpRegReg(kOpCmp, left_op, right_op);  // Same?
22490969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  LIR* it = OpIT(code, "E");   // if-convert the test
22590969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  LoadConstant(rs_dest, true_val);      // .eq case - load true
22690969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  LoadConstant(rs_dest, false_val);     // .eq case - load true
22790969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe  OpEndIT(it);
22890969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe}
22990969af6deb19b1dbe356d62fe68d8f5698d3d8fAndreas Gampe
2302ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstromvoid ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
231f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee  RegLocation rl_result;
2321fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
2331fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_dest = mir_graph_->GetDest(mir);
234a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  // Avoid using float regs here.
235a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
236a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
237a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  rl_src = LoadValue(rl_src, src_reg_class);
238a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko  ConditionCode ccode = mir->meta.ccode;
239f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee  if (mir->ssa_rep->num_uses == 1) {
240f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    // CONST case
241f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    int true_val = mir->dalvikInsn.vB;
242f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    int false_val = mir->dalvikInsn.vC;
243a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_result = EvalLoc(rl_dest, result_reg_class, true);
244a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    // Change kCondNe to kCondEq for the special cases below.
245a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    if (ccode == kCondNe) {
246a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko      ccode = kCondEq;
247a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko      std::swap(true_val, false_val);
248a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    }
249a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    bool cheap_false_val = InexpensiveConstantInt(false_val);
250a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
2512700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
2528dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko      DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
2533da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
2542700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadConstant(rl_result.reg, false_val);
2553da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      OpEndIT(it);  // Add a scheduling barrier to keep the IT shadow intact
256a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
2572700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
2588dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko      DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
2593da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      LIR* it = OpIT(kCondLs, "");
2602700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadConstant(rl_result.reg, false_val);
2613da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      OpEndIT(it);  // Add a scheduling barrier to keep the IT shadow intact
262a1a7074eb8256d101f7b5d256cda26d7de6ce6ceVladimir Marko    } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
2632700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegImm(kOpCmp, rl_src.reg, 0);
2643da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      LIR* it = OpIT(ccode, "E");
2652700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadConstant(rl_result.reg, true_val);
2662700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadConstant(rl_result.reg, false_val);
2673da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      OpEndIT(it);  // Add a scheduling barrier to keep the IT shadow intact
268f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    } else {
269f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee      // Unlikely case - could be tuned.
270a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee      RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
271a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee      RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
2721fd3346740dfb7f47be9922312b68a4227fada96buzbee      LoadConstant(t_reg1, true_val);
2731fd3346740dfb7f47be9922312b68a4227fada96buzbee      LoadConstant(t_reg2, false_val);
2742700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegImm(kOpCmp, rl_src.reg, 0);
2753da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      LIR* it = OpIT(ccode, "E");
2762700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, t_reg1);
2772700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, t_reg2);
2783da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      OpEndIT(it);  // Add a scheduling barrier to keep the IT shadow intact
279f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    }
280f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee  } else {
281f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee    // MOVE case
2821fd3346740dfb7f47be9922312b68a4227fada96buzbee    RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
2831fd3346740dfb7f47be9922312b68a4227fada96buzbee    RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
284a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_true = LoadValue(rl_true, result_reg_class);
285a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_false = LoadValue(rl_false, result_reg_class);
286a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_result = EvalLoc(rl_dest, result_reg_class, true);
2872700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegImm(kOpCmp, rl_src.reg, 0);
2883da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison    LIR* it = nullptr;
28900e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee    if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) {  // Is the "true" case already in place?
2903da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      it = OpIT(NegateComparison(ccode), "");
2912700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, rl_false.reg);
29200e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee    } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) {  // False case in place?
2933da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      it = OpIT(ccode, "");
2942700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, rl_true.reg);
295252254b130067cd7a5071865e793966871ae0246buzbee    } else {  // Normal - select between the two.
2963da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison      it = OpIT(ccode, "E");
2972700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, rl_true.reg);
2982700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegCopy(rl_result.reg, rl_false.reg);
299252254b130067cd7a5071865e793966871ae0246buzbee    }
3003da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison    OpEndIT(it);  // Add a scheduling barrier to keep the IT shadow intact
301f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee  }
3021fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValue(rl_dest, rl_result);
303f662a7c625b1067f61b23c04459eba9b12bc027ebuzbee}
3044ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
3052ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstromvoid ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
3061fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
3071fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
3084ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // Normalize such that if either operand is constant, src2 will be constant.
309a894607bca7eb623bc957363e4b36f44cfeea1b6Vladimir Marko  ConditionCode ccode = mir->meta.ccode;
3104ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (rl_src1.is_const) {
31158af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko    std::swap(rl_src1, rl_src2);
3124ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    ccode = FlipComparisonOrder(ccode);
3134ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
3144ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (rl_src2.is_const) {
315082833c8d577db0b2bebc100602f31e4e971613ebuzbee    rl_src2 = UpdateLocWide(rl_src2);
3164ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    // Do special compare/branch against simple const operand if not already in registers.
3171fd3346740dfb7f47be9922312b68a4227fada96buzbee    int64_t val = mir_graph_->ConstantValueWide(rl_src2);
318082833c8d577db0b2bebc100602f31e4e971613ebuzbee    if ((rl_src2.location != kLocPhysReg) &&
3194ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee        ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
3201fd3346740dfb7f47be9922312b68a4227fada96buzbee      GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
3214ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      return;
3224ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
3234ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
3240d82948094d9a198e01aa95f64012bdedd5b6fc9buzbee  LIR* taken = &block_label_list_[bb->taken];
3250d82948094d9a198e01aa95f64012bdedd5b6fc9buzbee  LIR* not_taken = &block_label_list_[bb->fall_through];
3261fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3271fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src2 = LoadValueWide(rl_src2, kCoreReg);
3282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
329df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  switch (ccode) {
330efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondEq:
3311fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondNe, not_taken);
332efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
333efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondNe:
3341fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondNe, taken);
335efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
336efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondLt:
3371fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondLt, taken);
3381fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondGt, not_taken);
33958af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      ccode = kCondUlt;
340efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
341efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondLe:
3421fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondLt, taken);
3431fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondGt, not_taken);
344efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      ccode = kCondLs;
345efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
346efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondGt:
3471fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondGt, taken);
3481fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondLt, not_taken);
349efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      ccode = kCondHi;
350efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
351efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case kCondGe:
3521fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondGt, taken);
3531fd3346740dfb7f47be9922312b68a4227fada96buzbee      OpCondBranch(kCondLt, not_taken);
35458af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      ccode = kCondUge;
355efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
356efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    default:
357cbd6d44c0a94f3d26671b5325aa21bbf1335ffe8buzbee      LOG(FATAL) << "Unexpected ccode: " << ccode;
358efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
3592700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
3601fd3346740dfb7f47be9922312b68a4227fada96buzbee  OpCondBranch(ccode, taken);
361efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
362efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
363efc6369224b036a1fb77849f7ae65b3492c832c0buzbee/*
364efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * Generate a register comparison to an immediate and branch.  Caller
365efc6369224b036a1fb77849f7ae65b3492c832c0buzbee * is responsible for setting branch target field.
366efc6369224b036a1fb77849f7ae65b3492c832c0buzbee */
3672700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
3689522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe  LIR* branch = nullptr;
369fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  ArmConditionCode arm_cond = ArmConditionEncoding(cond);
370b48819db07f9a0992a72173380c24249d7fc648abuzbee  /*
371b48819db07f9a0992a72173380c24249d7fc648abuzbee   * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
372b48819db07f9a0992a72173380c24249d7fc648abuzbee   * compare-and-branch if zero is ideal if it will reach.  However, because null checks
3733a74d15ccc9a902874473ac9632e568b19b91b1cMingyao Yang   * branch forward to a slow path, they will frequently not reach - and thus have to
374b48819db07f9a0992a72173380c24249d7fc648abuzbee   * be converted to a long form during assembly (which will trigger another assembly
375b48819db07f9a0992a72173380c24249d7fc648abuzbee   * pass).  Here we estimate the branch distance for checks, and if large directly
376b48819db07f9a0992a72173380c24249d7fc648abuzbee   * generate the long form in an attempt to avoid an extra assembly pass.
3773a74d15ccc9a902874473ac9632e568b19b91b1cMingyao Yang   * TODO: consider interspersing slowpaths in code following unconditional branches.
378b48819db07f9a0992a72173380c24249d7fc648abuzbee   */
379b48819db07f9a0992a72173380c24249d7fc648abuzbee  bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
380b48819db07f9a0992a72173380c24249d7fc648abuzbee  skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
3819522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe  if (!skip && reg.Low8() && (check_value == 0)) {
3829522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe    if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
3839522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe      branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
3849522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe                       reg.GetReg(), 0);
3859522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe    } else if (arm_cond == kArmCondLs) {
3869522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe      // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
3879522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe      // This case happens for a bounds check of array[0].
3889522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe      branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
3899522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe    }
3909522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe  }
3919522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe
3929522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe  if (branch == nullptr) {
3932247984899247b1402408d39731ff64048f0e274Vladimir Marko    OpRegImm(kOpCmp, reg, check_value);
3941fd3346740dfb7f47be9922312b68a4227fada96buzbee    branch = NewLIR2(kThumbBCond, 0, arm_cond);
395efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
3969522af985466b2a05ef5cdede0808777dea7236eAndreas Gampe
397efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  branch->target = target;
398efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  return branch;
399efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
40002031b185b4653e6c72e21f7a51238b903f6d638buzbee
4012700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
402efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  LIR* res;
403efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  int opcode;
4042700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  // If src or dest is a pair, we'll be using low reg.
4052700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  if (r_dest.IsPair()) {
4062700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    r_dest = r_dest.GetLow();
4072700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  }
4082700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  if (r_src.IsPair()) {
4092700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    r_src = r_src.GetLow();
4102700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  }
411091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  if (r_dest.IsFloat() || r_src.IsFloat())
4121fd3346740dfb7f47be9922312b68a4227fada96buzbee    return OpFpRegCopy(r_dest, r_src);
413091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  if (r_dest.Low8() && r_src.Low8())
414efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    opcode = kThumbMovRR;
415091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  else if (!r_dest.Low8() && !r_src.Low8())
416efc6369224b036a1fb77849f7ae65b3492c832c0buzbee     opcode = kThumbMovRR_H2H;
417091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  else if (r_dest.Low8())
418efc6369224b036a1fb77849f7ae65b3492c832c0buzbee     opcode = kThumbMovRR_H2L;
419efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  else
420efc6369224b036a1fb77849f7ae65b3492c832c0buzbee     opcode = kThumbMovRR_L2H;
4212700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
4221fd3346740dfb7f47be9922312b68a4227fada96buzbee  if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
423fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee    res->flags.is_nop = true;
424efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
425efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  return res;
426efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
427efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
4287a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbeevoid ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
4297a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee  if (r_dest != r_src) {
4307a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee    LIR* res = OpRegCopyNoInsert(r_dest, r_src);
4317a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee    AppendLIR(res);
4327a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee  }
433efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
434efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
4352700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeevoid ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
4367a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee  if (r_dest != r_src) {
437091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool dest_fp = r_dest.IsFloat();
438091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool src_fp = r_src.IsFloat();
439091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    DCHECK(r_dest.Is64Bit());
440091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    DCHECK(r_src.Is64Bit());
4417a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee    if (dest_fp) {
4427a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee      if (src_fp) {
443091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        OpRegCopy(r_dest, r_src);
4447a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee      } else {
445091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
4467a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee      }
447efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    } else {
4487a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee      if (src_fp) {
449091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
450efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      } else {
4517a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee        // Handle overlap
4527a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee        if (r_src.GetHighReg() == r_dest.GetLowReg()) {
4537a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee          DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
4547a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee          OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
4557a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee          OpRegCopy(r_dest.GetLow(), r_src.GetLow());
4567a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee        } else {
4577a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee          OpRegCopy(r_dest.GetLow(), r_src.GetLow());
4587a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee          OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
4597a11ab09f93f54b1c07c0bf38dd65ed322e86bc6buzbee        }
460efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      }
461efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    }
462efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
463efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
464efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
465efc6369224b036a1fb77849f7ae65b3492c832c0buzbee// Table of magic divisors
466efc6369224b036a1fb77849f7ae65b3492c832c0buzbeestruct MagicTable {
467efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  uint32_t magic;
468efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  uint32_t shift;
469efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  DividePattern pattern;
470efc6369224b036a1fb77849f7ae65b3492c832c0buzbee};
471efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
472fa57c47f1b72916371a9c2d5c1389219bce655b4buzbeestatic const MagicTable magic_table[] = {
473efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0, 0, DivideNone},        // 0
474efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0, 0, DivideNone},        // 1
475efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0, 0, DivideNone},        // 2
476efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x55555556, 0, Divide3},  // 3
477efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0, 0, DivideNone},        // 4
478efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x66666667, 1, Divide5},  // 5
479efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x2AAAAAAB, 0, Divide3},  // 6
480efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x92492493, 2, Divide7},  // 7
481efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0, 0, DivideNone},        // 8
482efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x38E38E39, 1, Divide5},  // 9
483efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x66666667, 2, Divide5},  // 10
484efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x2E8BA2E9, 1, Divide5},  // 11
485efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x2AAAAAAB, 1, Divide5},  // 12
486efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x4EC4EC4F, 2, Divide5},  // 13
487efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x92492493, 3, Divide7},  // 14
488efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  {0x88888889, 3, Divide7},  // 15
489efc6369224b036a1fb77849f7ae65b3492c832c0buzbee};
490efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
491efc6369224b036a1fb77849f7ae65b3492c832c0buzbee// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
49211b63d13f0a3be0f74390b66b58614a37f9aa6c1buzbeebool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
4932ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                    RegLocation rl_src, RegLocation rl_dest, int lit) {
494fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
495efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    return false;
496efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
497fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  DividePattern pattern = magic_table[lit].pattern;
498efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  if (pattern == DivideNone) {
499efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    return false;
500efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
501efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
5022700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage r_magic = AllocTemp();
5031fd3346740dfb7f47be9922312b68a4227fada96buzbee  LoadConstant(r_magic, magic_table[lit].magic);
5041fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src = LoadValue(rl_src, kCoreReg);
5051fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
5062700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage r_hi = AllocTemp();
5072700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage r_lo = AllocTemp();
508f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
509f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  // rl_dest and rl_src might overlap.
510f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  // Reuse r_hi to save the div result for reminder case.
511f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
512f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
5132700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
514df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  switch (pattern) {
515efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case Divide3:
516f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
517efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
518efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case Divide5:
5192700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
520f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
521e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers                       EncodeShift(kArmAsr, magic_table[lit].shift));
522efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
523efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    case Divide7:
5242700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegReg(kOpAdd, r_hi, rl_src.reg);
5252700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
526f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
527e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers                       EncodeShift(kArmAsr, magic_table[lit].shift));
528efc6369224b036a1fb77849f7ae65b3492c832c0buzbee      break;
529efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    default:
530cbd6d44c0a94f3d26671b5325aa21bbf1335ffe8buzbee      LOG(FATAL) << "Unexpected pattern: " << pattern;
531efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
532f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
533f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  if (!is_div) {
534f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    // div_result = src / lit
535f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    // tmp1 = div_result * lit
536f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    // dest = src - tmp1
537f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    RegStorage tmp1 = r_lo;
538f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    EasyMultiplyOp ops[2];
539f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
540f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
541f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    DCHECK_NE(canEasyMultiply, false);
542f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
543f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
544f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
545f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  }
546f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
5471fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValue(rl_dest, rl_result);
548efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  return true;
549efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
550efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
551e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
552e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogersbool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
55356e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe  if (lit == 0) {
55456e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe    // Special case for *divide-by-zero*. The ops won't actually be used to generate code, as
55556e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe    // GenArithOpIntLit will directly generate exception-throwing code, and multiply-by-zero will
55656e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe    // have been optimized away earlier.
55756e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe    op->op = kOpInvalid;
55856e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe    return true;
55956e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe  }
56056e7666fe0df2c1a16b2cc6d12562c3e82199c0dAndreas Gampe
561e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (IsPowerOfTwo(lit)) {
562e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->op = kOpLsl;
563e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->shift = LowestSetBit(lit);
564e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
565e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
566e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
567e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (IsPowerOfTwo(lit - 1)) {
568e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->op = kOpAdd;
569e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->shift = LowestSetBit(lit - 1);
570e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
571e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
572e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
573e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (IsPowerOfTwo(lit + 1)) {
574e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->op = kOpRsub;
575e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    op->shift = LowestSetBit(lit + 1);
576e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
577e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
578e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
579e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  op->op = kOpInvalid;
580f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  op->shift = 0;
581e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  return false;
582e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers}
583e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
584e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
585e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogersbool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
586e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  GetEasyMultiplyOp(lit, &ops[0]);
587e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (GetEasyMultiplyOp(lit, &ops[0])) {
588e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].op = kOpInvalid;
589f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    ops[1].shift = 0;
590e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
591e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
592e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
593e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  int lit1 = lit;
594e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  uint32_t shift = LowestSetBit(lit1);
595e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
596e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].op = kOpLsl;
597e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].shift = shift;
598e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
599e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
600e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
601e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  lit1 = lit - 1;
602e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  shift = LowestSetBit(lit1);
603e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
604e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].op = kOpAdd;
605e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].shift = shift;
606e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
607e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
608e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
609e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  lit1 = lit + 1;
610e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  shift = LowestSetBit(lit1);
611e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
612e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].op = kOpRsub;
613e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    ops[1].shift = shift;
614e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return true;
615e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
616e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
617e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  return false;
618e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers}
619e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
620f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu// Generate instructions to do multiply.
621f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu// Additional temporary register is required,
622f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu// if it need to generate 2 instructions and src/dest overlap.
623e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogersvoid ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
624f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  // tmp1 = ( src << shift1) + [ src | -src | 0 ]
625f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  // dest = (tmp1 << shift2) + [ src | -src | 0 ]
626f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
627f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  RegStorage r_tmp1;
628f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  if (ops[1].op == kOpInvalid) {
629f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    r_tmp1 = r_dest;
630f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  } else if (r_dest.GetReg() != r_src.GetReg()) {
631f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    r_tmp1 = r_dest;
632f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  } else {
633f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    r_tmp1 = AllocTemp();
634f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  }
635f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
636f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  switch (ops[0].op) {
637e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    case kOpLsl:
638f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
639e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      break;
640e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    case kOpAdd:
641f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
642e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      break;
643e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    case kOpRsub:
644f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
645e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      break;
646e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    default:
647f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      DCHECK_EQ(ops[0].op, kOpInvalid);
648f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      break;
649f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  }
650f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu
651f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu  switch (ops[1].op) {
652f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    case kOpInvalid:
653f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      return;
654f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    case kOpLsl:
655f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
656f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      break;
657f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    case kOpAdd:
658f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
659f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      break;
660f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    case kOpRsub:
661f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
662f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      break;
663f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu    default:
664f9719f9abbea060e086fe1304d72be50cbc8808eZheng Xu      LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
665e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      break;
666e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
667e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers}
668e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
669e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogersbool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
670e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  EasyMultiplyOp ops[2];
671e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
672e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  if (!GetEasyMultiplyTwoOps(lit, ops)) {
673e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers    return false;
674e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  }
675e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
676e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  rl_src = LoadValue(rl_src, kCoreReg);
677e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
678e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
679e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
680e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  StoreValue(rl_dest, rl_result);
681e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  return true;
682e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers}
683e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers
6842bf31e67694da24a19fc1f328285cebb1a4b9964Mark MendellRegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
6852bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell                      RegLocation rl_src2, bool is_div, bool check_zero) {
6862bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell  LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
6872bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell  return rl_dest;
6882bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell}
6892bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell
6902bf31e67694da24a19fc1f328285cebb1a4b9964Mark MendellRegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
6912bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell  LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
6922bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell  return rl_dest;
6932bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell}
6942bf31e67694da24a19fc1f328285cebb1a4b9964Mark Mendell
6952700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeRegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
6967020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
6977020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
6987020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  // Put the literal in a temp.
6992700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage lit_temp = AllocTemp();
7007020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  LoadConstant(lit_temp, lit);
7017020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  // Use the generic case for div/rem with arg2 in a register.
7027020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
7037020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
7047020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  FreeTemp(lit_temp);
7057020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
7067020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  return rl_result;
707efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
708efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
7092700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeRegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
7102ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                  bool is_div) {
7117020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
7127020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  if (is_div) {
7137020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    // Simple case, use sdiv instruction.
7142700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
7157020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  } else {
7167020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    // Remainder case, use the following code:
7177020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    // temp = reg1 / reg2      - integer division
7187020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    // temp = temp * reg2
7197020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    // dest = reg1 - temp
7207020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
7212700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage temp = AllocTemp();
7227020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    OpRegRegReg(kOpDiv, temp, reg1, reg2);
7237020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    OpRegReg(kOpMul, temp, reg2);
7242700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
7257020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison    FreeTemp(temp);
7267020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  }
7277020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
7287020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  return rl_result;
729efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
730efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
73123abec955e2e733999a1e2c30e4e384e46e5dde4Serban Constantinescubool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
7321fd3346740dfb7f47be9922312b68a4227fada96buzbee  DCHECK_EQ(cu_->instruction_set, kThumb2);
73323abec955e2e733999a1e2c30e4e384e46e5dde4Serban Constantinescu  if (is_long) {
73423abec955e2e733999a1e2c30e4e384e46e5dde4Serban Constantinescu    return false;
73523abec955e2e733999a1e2c30e4e384e46e5dde4Serban Constantinescu  }
736fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  RegLocation rl_src1 = info->args[0];
737fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  RegLocation rl_src2 = info->args[1];
7381fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src1 = LoadValue(rl_src1, kCoreReg);
7391fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src2 = LoadValue(rl_src2, kCoreReg);
7401fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_dest = InlineTarget(info);
7411fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
7422700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
7433da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
7442700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
7452700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
7463da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  OpEndIT(it);
7471fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValue(rl_dest, rl_result);
748efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  return true;
749efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
750efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
751e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Markobool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
752e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_src_address = info->args[0];  // long address
7532700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  rl_src_address = NarrowRegLoc(rl_src_address);  // ignore high half in info->args[1]
754e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_dest = InlineTarget(info);
755e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
756e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
757695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee  if (size == k64) {
758e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
7592700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
760695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee      Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
761695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee      Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
762e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    } else {
763695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee      Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
764695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee      Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
765e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    }
766e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    StoreValueWide(rl_dest, rl_result);
767e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  } else {
768695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee    DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
769e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
7703c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
771e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    StoreValue(rl_dest, rl_result);
772e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  }
773e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  return true;
774e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko}
775e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko
776e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Markobool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
777e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_src_address = info->args[0];  // long address
7782700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  rl_src_address = NarrowRegLoc(rl_src_address);  // ignore high half in info->args[1]
779e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_src_value = info->args[2];  // [size] value
780e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
781695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee  if (size == k64) {
782e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
783e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
7843c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
7853c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
786e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  } else {
787695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee    DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
788e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
789e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko    RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
7903c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
791e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  }
792e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko  return true;
793e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko}
794e508a2090b19fe705fbc6b99d76474037a74bbfbVladimir Marko
79548f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm// Generate a CAS with memory_order_seq_cst semantics.
7961c282e2b9a9b432e132b2c332f861cad9feb4a73Vladimir Markobool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
7971fd3346740dfb7f47be9922312b68a4227fada96buzbee  DCHECK_EQ(cu_->instruction_set, kThumb2);
798fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  // Unused - RegLocation rl_src_unsafe = info->args[0];
7991c282e2b9a9b432e132b2c332f861cad9feb4a73Vladimir Marko  RegLocation rl_src_obj = info->args[1];  // Object - known non-null
8001c282e2b9a9b432e132b2c332f861cad9feb4a73Vladimir Marko  RegLocation rl_src_offset = info->args[2];  // long low
8012700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  rl_src_offset = NarrowRegLoc(rl_src_offset);  // ignore high half in info->args[3]
8021c282e2b9a9b432e132b2c332f861cad9feb4a73Vladimir Marko  RegLocation rl_src_expected = info->args[4];  // int, long or Object
8033e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // If is_long, high half is in info->args[5]
8043e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  RegLocation rl_src_new_value = info->args[is_long ? 6 : 5];  // int, long or Object
8053e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // If is_long, high half is in info->args[7]
8061fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_dest = InlineTarget(info);  // boolean place for result
807efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8083e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // We have only 5 temporary registers available and actually only 4 if the InlineTarget
8093e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
8103e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
8113e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
8123e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // into the same temps, reducing the number of required temps down to 5. We shall work
8133e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // around the potentially locked temp by using LR for r_ptr, unconditionally.
8143e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // TODO: Pass information about the need for more temps to the stack frame generation
8153e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // code so that we can rely on being able to allocate enough temps.
816091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
817091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  MarkTemp(rs_rARM_LR);
818091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  FreeTemp(rs_rARM_LR);
819091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  LockTemp(rs_rARM_LR);
8203e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  bool load_early = true;
8213e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  if (is_long) {
822091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
823091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        rl_src_expected.reg;
824091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
825091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        rl_src_new_value.reg;
826091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
827091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
8282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
8292700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
8303e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
8313e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    if (!expected_is_good_reg && !new_value_is_good_reg) {
8323e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      // None of expected/new_value is non-temp reg, need to load both late
8333e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      load_early = false;
8343e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      // Make sure they are not in the temp regs and the load will not be skipped.
8353e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      if (expected_is_core_reg) {
8362700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        FlushRegWide(rl_src_expected.reg);
8373e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        ClobberSReg(rl_src_expected.s_reg_low);
8383e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
8393e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        rl_src_expected.location = kLocDalvikFrame;
8403e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      }
8413e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      if (new_value_is_core_reg) {
8422700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        FlushRegWide(rl_src_new_value.reg);
8433e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        ClobberSReg(rl_src_new_value.s_reg_low);
8443e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
8453e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko        rl_src_new_value.location = kLocDalvikFrame;
8463e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko      }
8473e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    }
8483e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  }
849efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
85048f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm  // Prevent reordering with prior memory operations.
85148f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm  GenMemBarrier(kAnyStore);
852efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
853a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
8543e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  RegLocation rl_new_value;
8553e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  if (!is_long) {
856a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_new_value = LoadValue(rl_src_new_value);
8573e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  } else if (load_early) {
8583e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
8593e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  }
860efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8611c282e2b9a9b432e132b2c332f861cad9feb4a73Vladimir Marko  if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
862efc6369224b036a1fb77849f7ae65b3492c832c0buzbee    // Mark card for object assuming new value is stored.
8632700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    MarkGCCard(rl_new_value.reg, rl_object.reg);
864efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
865efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8661fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
867efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8682700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage r_ptr = rs_rARM_LR;
8692700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
870efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
871fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  // Free now unneeded rl_object and rl_offset to give more temps.
8721fd3346740dfb7f47be9922312b68a4227fada96buzbee  ClobberSReg(rl_object.s_reg_low);
873091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  FreeTemp(rl_object.reg);
8741fd3346740dfb7f47be9922312b68a4227fada96buzbee  ClobberSReg(rl_offset.s_reg_low);
875091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  FreeTemp(rl_offset.reg);
876efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8773e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  RegLocation rl_expected;
8783e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  if (!is_long) {
879a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    rl_expected = LoadValue(rl_src_expected);
8803e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  } else if (load_early) {
8813e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
8823e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  } else {
88300e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee    // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
884091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    RegStorage low_reg = AllocTemp();
885091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    RegStorage high_reg = AllocTemp();
886091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
88700e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee    rl_expected = rl_new_value;
8883e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  }
8893e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
8903e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // do {
8913e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  //   tmp = [r_ptr] - expected;
8923e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
8933e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // result = tmp != 0;
894efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
8952700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage r_tmp = AllocTemp();
8962de2aa1a96dfa5bebc004f29b5dbfafd37039ceeJeff Hao  LIR* target = NewLIR0(kPseudoTargetLabel);
8973e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
8983da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  LIR* it = nullptr;
8993e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  if (is_long) {
9002700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage r_tmp_high = AllocTemp();
9013e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    if (!load_early) {
9022700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadValueDirectWide(rl_src_expected, rl_expected.reg);
9033e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    }
9042700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
9052700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
9062700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
9073e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    if (!load_early) {
9082700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
9093e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    }
9103e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    // Make sure we use ORR that sets the ccode
911091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    if (r_tmp.Low8() && r_tmp_high.Low8()) {
9122700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
9133e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    } else {
9142700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
9153e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    }
9163e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko    FreeTemp(r_tmp_high);  // Now unneeded
9173e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
9188dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko    DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
9193da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison    it = OpIT(kCondEq, "T");
9202700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    NewLIR4(kThumb2Strexd /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetLowReg(), rl_new_value.reg.GetHighReg(), r_ptr.GetReg());
9213e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
9223e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  } else {
9232700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
9242700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegReg(kOpSub, r_tmp, rl_expected.reg);
9258dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko    DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
9263da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison    it = OpIT(kCondEq, "T");
9272700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
9283e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  }
9293e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
9303e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // Still one conditional left from OpIT(kCondEq, "T") from either branch
9313e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  OpRegImm(kOpCmp /* eq */, r_tmp, 1);
9323da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  OpEndIT(it);
93343a065ce1dda78e963868f9753a6e263721af927Dave Allison
9342de2aa1a96dfa5bebc004f29b5dbfafd37039ceeJeff Hao  OpCondBranch(kCondEq, target);
935efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
9363e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  if (!load_early) {
9372700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    FreeTemp(rl_expected.reg);  // Now unneeded.
9383e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  }
9393e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
94048f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm  // Prevent reordering with subsequent memory operations.
94148f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm  GenMemBarrier(kLoadAny);
94248f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm
9433e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // result := (tmp1 != 0) ? 0 : 1;
9443e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
9452700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
9468dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko  DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
9473da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  it = OpIT(kCondUlt, "");
9482700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  LoadConstant(rl_result.reg, 0); /* cc */
9493e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  FreeTemp(r_tmp);  // Now unneeded.
9503da67a558f1fd3d8a157d8044d521753f3f99ac8Dave Allison  OpEndIT(it);     // Barrier to terminate OpIT.
9513e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko
9521fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValue(rl_dest, rl_result);
953efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
9543e5af82ae1a2cd69b7b045ac008ac3b394d17f41Vladimir Marko  // Now, restore lr to its non-temp status.
955091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  Clobber(rs_rARM_LR);
956091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  UnmarkTemp(rs_rARM_LR);
957efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  return true;
958efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
959efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
960fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xubool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
961fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  constexpr int kLargeArrayThreshold = 256;
962fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
963fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegLocation rl_src = info->args[0];
964fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegLocation rl_src_pos = info->args[1];
965fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegLocation rl_dst = info->args[2];
966fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegLocation rl_dst_pos = info->args[3];
967fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegLocation rl_length = info->args[4];
968fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Compile time check, handle exception by non-inline method to reduce related meta-data.
969fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
970fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu      (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
971fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu      (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
972fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu    return false;
973fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  }
974fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
975fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  ClobberCallerSave();
976fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LockCallTemps();  // Prepare for explicit register usage.
977fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LockTemp(rs_r12);
978fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_src = rs_r0;
979fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_dst = rs_r1;
980fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_src, rs_src);
981fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_dst, rs_dst);
982fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
983fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Handle null pointer exception in slow-path.
984fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
985fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
986fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Handle potential overlapping in slow-path.
987fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
988fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Handle exception or big length in slow-path.
989fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_length = rs_r2;
990fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_length, rs_length);
991fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
992fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Src bounds check.
993fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_pos = rs_r3;
994fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_arr_length = rs_r12;
995fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_src_pos, rs_pos);
996fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
997fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
998fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpSub, rs_arr_length, rs_pos);
999fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1000fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Dst bounds check.
1001fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_dst_pos, rs_pos);
1002fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1003fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1004fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpSub, rs_arr_length, rs_pos);
1005fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1006fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1007fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Everything is checked now.
1008fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1009fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpAdd, rs_dst, rs_pos);
1010fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpAdd, rs_dst, rs_pos);
1011fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1012fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadValueDirectFixed(rl_src_pos, rs_pos);
1013fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpAdd, rs_src, rs_pos);
1014fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegReg(kOpAdd, rs_src, rs_pos);
1015fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1016fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  RegStorage rs_tmp = rs_pos;
1017fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1018fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1019fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Copy one element.
1020fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1021fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1022fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegImm(kOpSub, rs_length, 2);
1023fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1024fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1025fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1026fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  // Copy two elements.
1027fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1028fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1029fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpRegImm(kOpSub, rs_length, 4);
1030fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1031fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1032fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  OpUnconditionalBranch(begin_loop);
1033fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1034fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1035fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1036fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  LIR* return_point = NewLIR0(kPseudoTargetLabel);
1037fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1038fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  src_check_branch->target = check_failed;
1039fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  dst_check_branch->target = check_failed;
1040fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  src_dst_same->target = check_failed;
1041fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  len_neg_or_too_big->target = check_failed;
1042fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  src_pos_negative->target = check_failed;
1043fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  src_bad_len->target = check_failed;
1044fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  dst_pos_negative->target = check_failed;
1045fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  dst_bad_len->target = check_failed;
1046fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  jmp_to_begin_loop->target = begin_loop;
1047fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  jmp_to_ret->target = return_point;
1048fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1049fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  AddIntrinsicSlowPath(info, launchpad_branch, return_point);
105002ff2d4187249d26fabe8e5eacc27b99984ee353Serguei Katkov  ClobberCallerSave();  // We must clobber everything because slow path will return here
1051fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
1052fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu  return true;
1053fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu}
1054fa9c8ec37c66574654e448513e1bb59af7cb9365Zheng Xu
10552700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
10562700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
1057efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1058efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
10592700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
1060091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
1061efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1062efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
10632700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
1064091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
1065efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1066efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
10671fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
106802031b185b4653e6c72e21f7a51238b903f6d638buzbee                                               RegLocation rl_result, int lit,
10692ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                               int first_bit, int second_bit) {
1070e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers  OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
1071fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee                   EncodeShift(kArmLsl, second_bit - first_bit));
1072fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  if (first_bit != 0) {
10732700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
1074efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
1075efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1076efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1077e643a179cf5585ba6bafdd4fa51730d9f50c06f6Mingyao Yangvoid ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
10782700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  DCHECK(reg.IsPair());   // TODO: support k64BitSolo.
10792700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage t_reg = AllocTemp();
10802700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
10811fd3346740dfb7f47be9922312b68a4227fada96buzbee  FreeTemp(t_reg);
1082e643a179cf5585ba6bafdd4fa51730d9f50c06f6Mingyao Yang  GenDivZeroCheck(kCondEq);
1083efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1084efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1085efc6369224b036a1fb77849f7ae65b3492c832c0buzbee// Test suspend flag, return target of taken suspend branch
10862ce745c06271d5223d57dbf08117b20d5b60694aBrian CarlstromLIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
108704f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin#ifdef ARM_R4_SUSPEND_FLAG
1088091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
10891fd3346740dfb7f47be9922312b68a4227fada96buzbee  return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
109004f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin#else
109104f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin  RegStorage t_reg = AllocTemp();
109204f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin  LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
109304f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin    t_reg, kUnsignedHalf);
109404f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin  LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
109504f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin    0, target);
109604f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin  FreeTemp(t_reg);
109704f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin  return cmp_branch;
109804f4d8abe45d6e79eca983e057de76aea24b7df9Wei Jin#endif
1099efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1100efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1101efc6369224b036a1fb77849f7ae65b3492c832c0buzbee// Decrement register and branch on condition
11022700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbeeLIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
1103efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  // Combine sub & test using sub setflags encoding here
1104dbb8c49d540edd2a39076093163c7218f03aa502Vladimir Marko  OpRegRegImm(kOpSub, reg, reg, 1);  // For value == 1, this should set flags.
11058dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko  DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
11061fd3346740dfb7f47be9922312b68a4227fada96buzbee  return OpCondBranch(c_code, target);
1107efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1108efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1109b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampebool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
1110efc6369224b036a1fb77849f7ae65b3492c832c0buzbee#if ANDROID_SMP != 0
111199ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
111299ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  LIR* barrier = last_lir_insn_;
111399ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru
1114fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  int dmb_flavor;
11151bc37c60da71c923ea9a2e99d31ba1b3d76d79a8buzbee  // TODO: revisit Arm barrier kinds
1116fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee  switch (barrier_kind) {
111748f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm    case kAnyStore: dmb_flavor = kISH; break;
111848f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm    case kLoadAny: dmb_flavor = kISH; break;
1119b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers    case kStoreStore: dmb_flavor = kISHST; break;
112048f5c47907654350ce30a8dfdda0e977f5d3d39fHans Boehm    case kAnyAny: dmb_flavor = kISH; break;
11211bc37c60da71c923ea9a2e99d31ba1b3d76d79a8buzbee    default:
1122fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee      LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1123fa57c47f1b72916371a9c2d5c1389219bce655b4buzbee      dmb_flavor = kSY;  // quiet gcc.
11241bc37c60da71c923ea9a2e99d31ba1b3d76d79a8buzbee      break;
11251bc37c60da71c923ea9a2e99d31ba1b3d76d79a8buzbee  }
112699ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru
1127b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  bool ret = false;
1128b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe
112999ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  // If the same barrier already exists, don't generate another.
113099ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  if (barrier == nullptr
113199ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru      || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
113299ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru    barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
1133b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe    ret = true;
113499ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  }
113599ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru
113699ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
113799ad7230ccaace93bf323dea9790f35fe991a4a2Razvan A Lupusoru  DCHECK(!barrier->flags.use_def_invalid);
11388dea81ca9c0201ceaa88086b927a5838a06a3e69Vladimir Marko  barrier->u.m.def_mask = &kEncodeAll;
1139b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  return ret;
1140b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe#else
1141b14329f90f725af0f67c45dfcb94933a426d63ceAndreas Gampe  return false;
1142efc6369224b036a1fb77849f7ae65b3492c832c0buzbee#endif
1143efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1144efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
11452ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstromvoid ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
11461fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src = LoadValueWide(rl_src, kCoreReg);
11471fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
11482700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage z_reg = AllocTemp();
11491fd3346740dfb7f47be9922312b68a4227fada96buzbee  LoadConstantNoClobber(z_reg, 0);
1150efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  // Check for destructive overlap
11512700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
11522700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage t_reg = AllocTemp();
1153c375e52e813857ad4c726bfb19ef4114e11095dfVladimir Marko    OpRegCopy(t_reg, rl_result.reg.GetLow());
11542700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
11552700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
11561fd3346740dfb7f47be9922312b68a4227fada96buzbee    FreeTemp(t_reg);
1157efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  } else {
11582700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
11592700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
1160efc6369224b036a1fb77849f7ae65b3492c832c0buzbee  }
11611fd3346740dfb7f47be9922312b68a4227fada96buzbee  FreeTemp(z_reg);
11621fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValueWide(rl_dest, rl_result);
1163efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1164efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1165e02d48fb24747f90fd893e1c3572bb3c500afcedMark Mendellvoid ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1166e02d48fb24747f90fd893e1c3572bb3c500afcedMark Mendell                            RegLocation rl_src1, RegLocation rl_src2) {
11674ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    /*
1168d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     * tmp1     = src1.hi * src2.lo;  // src1.hi is no longer needed
1169d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     * dest     = src1.lo * src2.lo;
1170d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     * tmp1    += src1.lo * src2.hi;
1171d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     * dest.hi += tmp1;
1172d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     *
1173d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu     * To pull off inline multiply, we have a worst-case requirement of 7 temporary
11744ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * registers.  Normally for Arm, we get 5.  We can get to 6 by including
11754ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * lr in the temp set.  The only problematic case is all operands and result are
11764ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * distinct, and none have been promoted.  In that case, we can succeed by aggressively
11774ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * freeing operand temp registers after they are no longer needed.  All other cases
11784ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * can proceed normally.  We'll just punt on the case of the result having a misaligned
11794ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     * overlap with either operand and send that case to a runtime handler.
11804ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee     */
11814ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    RegLocation rl_result;
11821fd3346740dfb7f47be9922312b68a4227fada96buzbee    if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
11831fd3346740dfb7f47be9922312b68a4227fada96buzbee      FlushAllRegs();
1184984305917bf57b3f8d92965e4715a0370cc5bcfbAndreas Gampe      CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1185a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee      rl_result = GetReturnWide(kCoreReg);
11861fd3346740dfb7f47be9922312b68a4227fada96buzbee      StoreValueWide(rl_dest, rl_result);
11874ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      return;
11884ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
1189d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
1190d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1191d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1192d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
1193d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    int reg_status = 0;
11942700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage res_lo;
11952700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage res_hi;
11962700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1197091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1198091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1199091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1200d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    // Check if rl_dest is *not* either operand and we have enough temp registers.
1201d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1202d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        (dest_promoted || src1_promoted || src2_promoted)) {
1203d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      // In this case, we do not need to manually allocate temp registers for result.
1204d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      rl_result = EvalLoc(rl_dest, kCoreReg, true);
12052700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      res_lo = rl_result.reg.GetLow();
12062700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      res_hi = rl_result.reg.GetHigh();
1207d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    } else {
1208d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      res_lo = AllocTemp();
1209d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1210d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        // In this case, we have enough temp registers to be allocated for result.
1211d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        res_hi = AllocTemp();
1212d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        reg_status = 1;
1213d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      } else {
1214d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        // In this case, all temps are now allocated.
1215d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        // res_hi will be allocated after we can free src1_hi.
1216d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        reg_status = 2;
1217d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      }
1218d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    }
1219d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
12204ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    // Temporarily add LR to the temp pool, and assign it to tmp1
1221091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    MarkTemp(rs_rARM_LR);
1222091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    FreeTemp(rs_rARM_LR);
12232700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage tmp1 = rs_rARM_LR;
1224091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    LockTemp(rs_rARM_LR);
12254ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
12262700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    if (rl_src1.reg == rl_src2.reg) {
12272700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      DCHECK(res_hi.Valid());
12282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      DCHECK(res_lo.Valid());
12292700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
12302700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
12312700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee              rl_src1.reg.GetLowReg());
1232e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
12334ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    } else {
12342700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1235d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      if (reg_status == 2) {
12362700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        DCHECK(!res_hi.Valid());
12372700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
123800e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee        DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1239082833c8d577db0b2bebc100602f31e4e971613ebuzbee        // Will force free src1_hi, so must clobber.
1240082833c8d577db0b2bebc100602f31e4e971613ebuzbee        Clobber(rl_src1.reg);
1241091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee        FreeTemp(rl_src1.reg.GetHigh());
1242d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu        res_hi = AllocTemp();
12434ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
12442700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      DCHECK(res_hi.Valid());
12452700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      DCHECK(res_lo.Valid());
12462700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
12472700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee              rl_src1.reg.GetLowReg());
12482700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
12492700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee              tmp1.GetReg());
12502700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1251d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      if (reg_status == 2) {
1252082833c8d577db0b2bebc100602f31e4e971613ebuzbee        FreeTemp(rl_src1.reg.GetLow());
12534ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
12544ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
1255d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
12564ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    // Now, restore lr to its non-temp status.
1257d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    FreeTemp(tmp1);
1258091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    Clobber(rs_rARM_LR);
1259091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    UnmarkTemp(rs_rARM_LR);
1260d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
1261d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    if (reg_status != 0) {
1262d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      // We had manually allocated registers for rl_result.
1263d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu      // Now construct a RegLocation.
1264a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee      rl_result = GetReturnWide(kCoreReg);  // Just using as a template.
12652700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1266d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    }
1267d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu
1268d7f8e02041e9d16160bc81bd1fa19189bffc04b3Zheng Xu    StoreValueWide(rl_dest, rl_result);
12694ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee}
12704ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
1271c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampevoid ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
1272c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe                                RegLocation rl_src2) {
1273c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe  switch (opcode) {
1274c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe    case Instruction::MUL_LONG:
1275c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe    case Instruction::MUL_LONG_2ADDR:
1276c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe      GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1277c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe      return;
1278c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe    case Instruction::NEG_LONG:
1279c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe      GenNegLong(rl_dest, rl_src2);
1280c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe      return;
1281efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1282c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe    default:
1283c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe      break;
1284c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe  }
1285efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1286c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe  // Fallback for all other ops.
1287c76c614d681d187d815760eb909e5faf488a3c35Andreas Gampe  Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1288efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}
1289efc6369224b036a1fb77849f7ae65b3492c832c0buzbee
1290e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee/*
1291e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee * Generate array load
1292e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee */
12931fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
1294a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers                             RegLocation rl_index, RegLocation rl_dest, int scale) {
1295091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  RegisterClass reg_class = RegClassBySize(size);
12962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  int len_offset = mirror::Array::LengthOffset().Int32Value();
1297e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  int data_offset;
1298e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  RegLocation rl_result;
12994ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  bool constant_index = rl_index.is_const;
1300a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  rl_array = LoadValue(rl_array, kRefReg);
13014ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (!constant_index) {
13021fd3346740dfb7f47be9922312b68a4227fada96buzbee    rl_index = LoadValue(rl_index, kCoreReg);
13034ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
1304e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1305e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  if (rl_dest.wide) {
13062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1307e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  } else {
13082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1309e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1310e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
13114ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // If index is constant, just fold it into the data offset
13124ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (constant_index) {
13131fd3346740dfb7f47be9922312b68a4227fada96buzbee    data_offset += mir_graph_->ConstantValue(rl_index) << scale;
13144ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
13154ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
1316e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  /* null object? */
13172700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  GenNullCheck(rl_array.reg, opt_flags);
1318e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1319e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
13202700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage reg_len;
1321e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  if (needs_range_check) {
13221fd3346740dfb7f47be9922312b68a4227fada96buzbee    reg_len = AllocTemp();
1323e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    /* Get len */
1324695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee    Load32Disp(rl_array.reg, len_offset, reg_len);
1325b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison    MarkPossibleNullPointerException(opt_flags);
1326b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison  } else {
13272700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    ForceImplicitNullCheck(rl_array.reg, opt_flags);
1328e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
13294ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (rl_dest.wide || rl_dest.fp || constant_index) {
13302700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    RegStorage reg_ptr;
13314ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    if (constant_index) {
13322700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      reg_ptr = rl_array.reg;  // NOTE: must not alter reg_ptr in constant case.
13334ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    } else {
13344ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      // No special indexed operation, lea + load w/ displacement
1335a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee      reg_ptr = AllocTempRef();
1336e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
1337091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee      FreeTemp(rl_index.reg);
13384ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
13391fd3346740dfb7f47be9922312b68a4227fada96buzbee    rl_result = EvalLoc(rl_dest, reg_class, true);
1340e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1341e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (needs_range_check) {
13424ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if (constant_index) {
134380365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang        GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
13444ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else {
134580365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang        GenArrayBoundsCheck(rl_index.reg, reg_len);
13464ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
13471fd3346740dfb7f47be9922312b68a4227fada96buzbee      FreeTemp(reg_len);
1348e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
13493c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
1350455759b5702b9435b91d1b4dada22c4cce7cae3cVladimir Marko    MarkPossibleNullPointerException(opt_flags);
1351455759b5702b9435b91d1b4dada22c4cce7cae3cVladimir Marko    if (!constant_index) {
1352455759b5702b9435b91d1b4dada22c4cce7cae3cVladimir Marko      FreeTemp(reg_ptr);
1353455759b5702b9435b91d1b4dada22c4cce7cae3cVladimir Marko    }
1354e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (rl_dest.wide) {
13551fd3346740dfb7f47be9922312b68a4227fada96buzbee      StoreValueWide(rl_dest, rl_result);
1356e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    } else {
13571fd3346740dfb7f47be9922312b68a4227fada96buzbee      StoreValue(rl_dest, rl_result);
1358e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
1359e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  } else {
1360e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    // Offset base, then use indexed load
1361a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    RegStorage reg_ptr = AllocTempRef();
13622700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1363091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    FreeTemp(rl_array.reg);
13641fd3346740dfb7f47be9922312b68a4227fada96buzbee    rl_result = EvalLoc(rl_dest, reg_class, true);
1365e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1366e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (needs_range_check) {
136780365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang      GenArrayBoundsCheck(rl_index.reg, reg_len);
13681fd3346740dfb7f47be9922312b68a4227fada96buzbee      FreeTemp(reg_len);
1369e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
13702700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
1371b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison    MarkPossibleNullPointerException(opt_flags);
13721fd3346740dfb7f47be9922312b68a4227fada96buzbee    FreeTemp(reg_ptr);
13731fd3346740dfb7f47be9922312b68a4227fada96buzbee    StoreValue(rl_dest, rl_result);
1374e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1375e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee}
1376e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1377e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee/*
1378e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee * Generate array store
1379e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee *
1380e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee */
13811fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
1382a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers                             RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
1383091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  RegisterClass reg_class = RegClassBySize(size);
13842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  int len_offset = mirror::Array::LengthOffset().Int32Value();
13854ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  bool constant_index = rl_index.is_const;
1386e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1387a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  int data_offset;
1388695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee  if (size == k64 || size == kDouble) {
13892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1390e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  } else {
13912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1392e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1393e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
13944ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // If index is constant, just fold it into the data offset.
13954ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (constant_index) {
13961fd3346740dfb7f47be9922312b68a4227fada96buzbee    data_offset += mir_graph_->ConstantValue(rl_index) << scale;
13974ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
13984ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
1399a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee  rl_array = LoadValue(rl_array, kRefReg);
14004ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (!constant_index) {
14011fd3346740dfb7f47be9922312b68a4227fada96buzbee    rl_index = LoadValue(rl_index, kCoreReg);
14024ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
14034ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
14042700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage reg_ptr;
1405773aab1e8992b2834153eb23c976a4eb0da51a71Ian Rogers  bool allocated_reg_ptr_temp = false;
14064ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (constant_index) {
14072700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    reg_ptr = rl_array.reg;
1408091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee  } else if (IsTemp(rl_array.reg) && !card_mark) {
1409091cc408e9dc87e60fb64c61e186bea568fc3d3abuzbee    Clobber(rl_array.reg);
14102700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    reg_ptr = rl_array.reg;
1411e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  } else {
1412773aab1e8992b2834153eb23c976a4eb0da51a71Ian Rogers    allocated_reg_ptr_temp = true;
1413a0cd2d701f29e0bc6275f1b13c0edfd4ec391879buzbee    reg_ptr = AllocTempRef();
1414e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1415e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1416e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  /* null object? */
14172700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  GenNullCheck(rl_array.reg, opt_flags);
1418e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1419e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
14202700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee  RegStorage reg_len;
1421e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  if (needs_range_check) {
14221fd3346740dfb7f47be9922312b68a4227fada96buzbee    reg_len = AllocTemp();
14237934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    // NOTE: max live temps(4) here.
1424e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    /* Get len */
1425695d13a82d6dd801aaa57a22a9d4b3f6db0d0fdbbuzbee    Load32Disp(rl_array.reg, len_offset, reg_len);
1426b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison    MarkPossibleNullPointerException(opt_flags);
1427b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison  } else {
14282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    ForceImplicitNullCheck(rl_array.reg, opt_flags);
1429e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1430e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  /* at this point, reg_ptr points to array, 2 live temps */
14314ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (rl_src.wide || rl_src.fp || constant_index) {
1432e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (rl_src.wide) {
14331fd3346740dfb7f47be9922312b68a4227fada96buzbee      rl_src = LoadValueWide(rl_src, reg_class);
1434e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    } else {
14351fd3346740dfb7f47be9922312b68a4227fada96buzbee      rl_src = LoadValue(rl_src, reg_class);
1436e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
14374ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    if (!constant_index) {
1438e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers      OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
14394ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
1440e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (needs_range_check) {
14414ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if (constant_index) {
144280365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang        GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
14434ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else {
144480365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang        GenArrayBoundsCheck(rl_index.reg, reg_len);
14454ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
14461fd3346740dfb7f47be9922312b68a4227fada96buzbee      FreeTemp(reg_len);
1447e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
14484ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
14493c12c512faf6837844d5465b23b9410889e5eb11Andreas Gampe    StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
1450b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison    MarkPossibleNullPointerException(opt_flags);
1451e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  } else {
1452e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    /* reg_ptr -> array data */
14532700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
14541fd3346740dfb7f47be9922312b68a4227fada96buzbee    rl_src = LoadValue(rl_src, reg_class);
1455e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    if (needs_range_check) {
145680365d9bb947edef0eae0bfe62b9f7a239416e6bMingyao Yang      GenArrayBoundsCheck(rl_index.reg, reg_len);
14571fd3346740dfb7f47be9922312b68a4227fada96buzbee      FreeTemp(reg_len);
1458e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee    }
14592700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
1460b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison    MarkPossibleNullPointerException(opt_flags);
1461e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee  }
1462773aab1e8992b2834153eb23c976a4eb0da51a71Ian Rogers  if (allocated_reg_ptr_temp) {
14631fd3346740dfb7f47be9922312b68a4227fada96buzbee    FreeTemp(reg_ptr);
14644ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
1465a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  if (card_mark) {
14662700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee    MarkGCCard(rl_src.reg, rl_array.reg);
14676a791b26577a61fd14f4836c795d616869bd2957buzbee  }
1468e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee}
1469e6285f99a53a344efd6f8409ff5f43a3e80190dbbuzbee
1470a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers
14711fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
14722ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                   RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
14731fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src = LoadValueWide(rl_src, kCoreReg);
14744ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // Per spec, we only care about low 6 bits of shift amount.
14751fd3346740dfb7f47be9922312b68a4227fada96buzbee  int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
14764ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if (shift_amount == 0) {
14771fd3346740dfb7f47be9922312b68a4227fada96buzbee    StoreValueWide(rl_dest, rl_src);
1478a5954be0aac5edd892fb31a209960543d00e4500buzbee    return;
14794ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
14801fd3346740dfb7f47be9922312b68a4227fada96buzbee  if (BadOverlap(rl_src, rl_dest)) {
14811fd3346740dfb7f47be9922312b68a4227fada96buzbee    GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1482a5954be0aac5edd892fb31a209960543d00e4500buzbee    return;
14834ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
14841fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1485df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  switch (opcode) {
14864ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SHL_LONG:
14874ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SHL_LONG_2ADDR:
14884ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if (shift_amount == 1) {
14892700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
14902700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
14914ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else if (shift_amount == 32) {
14922700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
14932700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        LoadConstant(rl_result.reg.GetLow(), 0);
14944ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else if (shift_amount > 31) {
14952700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
14962700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        LoadConstant(rl_result.reg.GetLow(), 0);
14974ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else {
14982700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
1499e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers        OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
15004ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee                         EncodeShift(kArmLsr, 32 - shift_amount));
15012700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
15024ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
15034ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15044ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SHR_LONG:
15054ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SHR_LONG_2ADDR:
15064ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if (shift_amount == 32) {
15072700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
15082700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
15094ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else if (shift_amount > 31) {
15102700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
15112700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
15124ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else {
15132700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        RegStorage t_reg = AllocTemp();
15142700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
1515e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers        OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
15164ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee                         EncodeShift(kArmLsl, 32 - shift_amount));
15171fd3346740dfb7f47be9922312b68a4227fada96buzbee        FreeTemp(t_reg);
15182700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
15194ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
15204ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15214ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::USHR_LONG:
15224ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::USHR_LONG_2ADDR:
15234ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if (shift_amount == 32) {
15242700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
15252700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        LoadConstant(rl_result.reg.GetHigh(), 0);
15264ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else if (shift_amount > 31) {
15272700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
15282700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        LoadConstant(rl_result.reg.GetHigh(), 0);
15294ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      } else {
15302700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        RegStorage t_reg = AllocTemp();
15312700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
1532e2143c0a4af68c08e811885eb2f3ea5bfdb21ab6Ian Rogers        OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
15334ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee                         EncodeShift(kArmLsl, 32 - shift_amount));
15341fd3346740dfb7f47be9922312b68a4227fada96buzbee        FreeTemp(t_reg);
15352700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
15364ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
15374ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15384ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    default:
15394ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      LOG(FATAL) << "Unexpected case";
15404ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
15411fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValueWide(rl_dest, rl_result);
15424ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee}
15434ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
15441fd3346740dfb7f47be9922312b68a4227fada96buzbeevoid ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
15452ce745c06271d5223d57dbf08117b20d5b60694aBrian Carlstrom                                   RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
15464ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
15474ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    if (!rl_src2.is_const) {
15484ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      // Don't bother with special handling for subtract from immediate.
15491fd3346740dfb7f47be9922312b68a4227fada96buzbee      GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1550a5954be0aac5edd892fb31a209960543d00e4500buzbee      return;
15514ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
15524ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  } else {
15534ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    // Normalize
15544ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    if (!rl_src2.is_const) {
15554ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      DCHECK(rl_src1.is_const);
155658af1f9385742f70aca4fcb5e13aba53b8be2ef4Vladimir Marko      std::swap(rl_src1, rl_src2);
15574ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    }
15584ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
15591fd3346740dfb7f47be9922312b68a4227fada96buzbee  if (BadOverlap(rl_src1, rl_dest)) {
15601fd3346740dfb7f47be9922312b68a4227fada96buzbee    GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1561a5954be0aac5edd892fb31a209960543d00e4500buzbee    return;
15624ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
15634ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  DCHECK(rl_src2.is_const);
15641fd3346740dfb7f47be9922312b68a4227fada96buzbee  int64_t val = mir_graph_->ConstantValueWide(rl_src2);
15654ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  uint32_t val_lo = Low32Bits(val);
15664ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  uint32_t val_hi = High32Bits(val);
15674ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  int32_t mod_imm_lo = ModifiedImmediate(val_lo);
15684ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  int32_t mod_imm_hi = ModifiedImmediate(val_hi);
15694ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
15704ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
1571df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  switch (opcode) {
15724ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::ADD_LONG:
15734ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::ADD_LONG_2ADDR:
15744ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SUB_LONG:
15754ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SUB_LONG_2ADDR:
15764ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
15771fd3346740dfb7f47be9922312b68a4227fada96buzbee        GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1578a5954be0aac5edd892fb31a209960543d00e4500buzbee        return;
15794ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
15804ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15814ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    default:
15824ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15834ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
15841fd3346740dfb7f47be9922312b68a4227fada96buzbee  rl_src1 = LoadValueWide(rl_src1, kCoreReg);
15851fd3346740dfb7f47be9922312b68a4227fada96buzbee  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
15864ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
15874ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  switch (opcode) {
15884ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::ADD_LONG:
15894ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::ADD_LONG_2ADDR:
15902700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
159100e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee      NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
15924ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
15934ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::OR_LONG:
15944ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::OR_LONG_2ADDR:
15952700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
15962700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
15974ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
159800e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee      if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
15992700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
16004ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
16014ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
16024ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::XOR_LONG:
16034ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::XOR_LONG_2ADDR:
16042700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
16052700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
16064ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
16074ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::AND_LONG:
16084ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::AND_LONG_2ADDR:
16092700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
16102700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
16114ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
161200e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee      if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
16132700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee        OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
16144ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      }
16154ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
16164ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SUB_LONG_2ADDR:
16174ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    case Instruction::SUB_LONG:
16182700f7e1edbcd2518f4978e4cd0e05a4149f91b6buzbee      NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
161900e1ec6581b5b7b46ca4c314c2854e9caa647dd2Bill Buzbee      NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
16204ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      break;
16214ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee    default:
16224ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee      LOG(FATAL) << "Unexpected opcode " << opcode;
16234ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  }
16241fd3346740dfb7f47be9922312b68a4227fada96buzbee  StoreValueWide(rl_dest, rl_result);
16254ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee}
16264ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee
1627efc6369224b036a1fb77849f7ae65b3492c832c0buzbee}  // namespace art
1628