Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* This file contains codegen for the Thumb2 ISA. */ |
| 18 | |
| 19 | #include "arm_lir.h" |
| 20 | #include "codegen_arm.h" |
| 21 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 22 | #include "entrypoints/quick/quick_entrypoints.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | #include "mirror/array.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 27 | LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | OpRegReg(kOpCmp, src1, src2); |
| 29 | return OpCondBranch(cond, target); |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Generate a Thumb2 IT instruction, which can nullify up to |
| 34 | * four subsequent instructions based on a condition and its |
| 35 | * inverse. The condition applies to the first instruction, which |
| 36 | * is executed if the condition is met. The string "guide" consists |
| 37 | * of 0 to 3 chars, and applies to the 2nd through 4th instruction. |
| 38 | * A "T" means the instruction is executed if the condition is |
| 39 | * met, and an "E" means the instruction is executed if the condition |
| 40 | * is not met. |
| 41 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 42 | LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 43 | int mask; |
| 44 | int mask3 = 0; |
| 45 | int mask2 = 0; |
| 46 | int mask1 = 0; |
| 47 | ArmConditionCode code = ArmConditionEncoding(ccode); |
| 48 | int cond_bit = code & 1; |
| 49 | int alt_bit = cond_bit ^ 1; |
| 50 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 51 | // Note: case fallthroughs intentional |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 52 | switch (strlen(guide)) { |
| 53 | case 3: |
| 54 | mask1 = (guide[2] == 'T') ? cond_bit : alt_bit; |
| 55 | case 2: |
| 56 | mask2 = (guide[1] == 'T') ? cond_bit : alt_bit; |
| 57 | case 1: |
| 58 | mask3 = (guide[0] == 'T') ? cond_bit : alt_bit; |
| 59 | break; |
| 60 | case 0: |
| 61 | break; |
| 62 | default: |
| 63 | LOG(FATAL) << "OAT: bad case in OpIT"; |
| 64 | } |
| 65 | mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) | |
| 66 | (1 << (3 - strlen(guide))); |
| 67 | return NewLIR2(kThumb2It, code, mask); |
| 68 | } |
| 69 | |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 70 | void ArmMir2Lir::OpEndIT(LIR* it) { |
| 71 | // TODO: use the 'it' pointer to do some checks with the LIR, for example |
| 72 | // we could check that the number of instructions matches the mask |
| 73 | // in the IT instruction. |
| 74 | CHECK(it != nullptr); |
| 75 | GenBarrier(); |
| 76 | } |
| 77 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 78 | /* |
| 79 | * 64-bit 3way compare function. |
| 80 | * mov rX, #-1 |
| 81 | * cmp op1hi, op2hi |
| 82 | * blt done |
| 83 | * bgt flip |
| 84 | * sub rX, op1lo, op2lo (treat as unsigned) |
| 85 | * beq done |
| 86 | * ite hi |
| 87 | * mov(hi) rX, #-1 |
| 88 | * mov(!hi) rX, #1 |
| 89 | * flip: |
| 90 | * neg rX |
| 91 | * done: |
| 92 | */ |
buzbee | a1983d4 | 2014-04-07 12:35:39 -0700 | [diff] [blame] | 93 | void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 94 | LIR* target1; |
| 95 | LIR* target2; |
| 96 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 97 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 98 | RegStorage t_reg = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 99 | LoadConstant(t_reg, -1); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 100 | OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 101 | LIR* branch1 = OpCondBranch(kCondLt, NULL); |
| 102 | LIR* branch2 = OpCondBranch(kCondGt, NULL); |
buzbee | a1983d4 | 2014-04-07 12:35:39 -0700 | [diff] [blame] | 103 | OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | LIR* branch3 = OpCondBranch(kCondEq, NULL); |
| 105 | |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 106 | LIR* it = OpIT(kCondHi, "E"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 107 | NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | LoadConstant(t_reg, 1); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 109 | OpEndIT(it); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | |
| 111 | target2 = NewLIR0(kPseudoTargetLabel); |
| 112 | OpRegReg(kOpNeg, t_reg, t_reg); |
| 113 | |
| 114 | target1 = NewLIR0(kPseudoTargetLabel); |
| 115 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 116 | RegLocation rl_temp = LocCReturn(); // Just using as template, will change |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 117 | rl_temp.reg.SetReg(t_reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 118 | StoreValue(rl_dest, rl_temp); |
| 119 | FreeTemp(t_reg); |
| 120 | |
| 121 | branch1->target = target1; |
| 122 | branch2->target = target2; |
| 123 | branch3->target = branch1->target; |
| 124 | } |
| 125 | |
| 126 | void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 127 | int64_t val, ConditionCode ccode) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 128 | int32_t val_lo = Low32Bits(val); |
| 129 | int32_t val_hi = High32Bits(val); |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 130 | DCHECK_GE(ModifiedImmediate(val_lo), 0); |
| 131 | DCHECK_GE(ModifiedImmediate(val_hi), 0); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 132 | LIR* taken = &block_label_list_[bb->taken]; |
| 133 | LIR* not_taken = &block_label_list_[bb->fall_through]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 134 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 135 | RegStorage low_reg = rl_src1.reg.GetLow(); |
| 136 | RegStorage high_reg = rl_src1.reg.GetHigh(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 137 | |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 138 | if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 139 | RegStorage t_reg = AllocTemp(); |
| 140 | NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 141 | FreeTemp(t_reg); |
| 142 | OpCondBranch(ccode, taken); |
| 143 | return; |
| 144 | } |
| 145 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 146 | switch (ccode) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 147 | case kCondEq: |
| 148 | case kCondNe: |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 149 | OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 150 | break; |
| 151 | case kCondLt: |
| 152 | OpCmpImmBranch(kCondLt, high_reg, val_hi, taken); |
| 153 | OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 154 | ccode = kCondUlt; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 155 | break; |
| 156 | case kCondLe: |
| 157 | OpCmpImmBranch(kCondLt, high_reg, val_hi, taken); |
| 158 | OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken); |
| 159 | ccode = kCondLs; |
| 160 | break; |
| 161 | case kCondGt: |
| 162 | OpCmpImmBranch(kCondGt, high_reg, val_hi, taken); |
| 163 | OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken); |
| 164 | ccode = kCondHi; |
| 165 | break; |
| 166 | case kCondGe: |
| 167 | OpCmpImmBranch(kCondGt, high_reg, val_hi, taken); |
| 168 | OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 169 | ccode = kCondUge; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 170 | break; |
| 171 | default: |
| 172 | LOG(FATAL) << "Unexpected ccode: " << ccode; |
| 173 | } |
| 174 | OpCmpImmBranch(ccode, low_reg, val_lo, taken); |
| 175 | } |
| 176 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 177 | void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | RegLocation rl_result; |
| 179 | RegLocation rl_src = mir_graph_->GetSrc(mir, 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | RegLocation rl_dest = mir_graph_->GetDest(mir); |
| 181 | rl_src = LoadValue(rl_src, kCoreReg); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 182 | ConditionCode ccode = mir->meta.ccode; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 183 | if (mir->ssa_rep->num_uses == 1) { |
| 184 | // CONST case |
| 185 | int true_val = mir->dalvikInsn.vB; |
| 186 | int false_val = mir->dalvikInsn.vC; |
| 187 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 188 | // Change kCondNe to kCondEq for the special cases below. |
| 189 | if (ccode == kCondNe) { |
| 190 | ccode = kCondEq; |
| 191 | std::swap(true_val, false_val); |
| 192 | } |
| 193 | bool cheap_false_val = InexpensiveConstantInt(false_val); |
| 194 | if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 195 | OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 196 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 197 | LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 198 | LoadConstant(rl_result.reg, false_val); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 199 | OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 200 | } else if (cheap_false_val && ccode == kCondEq && true_val == 1) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 201 | OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 202 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 203 | LIR* it = OpIT(kCondLs, ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 204 | LoadConstant(rl_result.reg, false_val); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 205 | OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 206 | } else if (cheap_false_val && InexpensiveConstantInt(true_val)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 207 | OpRegImm(kOpCmp, rl_src.reg, 0); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 208 | LIR* it = OpIT(ccode, "E"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 209 | LoadConstant(rl_result.reg, true_val); |
| 210 | LoadConstant(rl_result.reg, false_val); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 211 | OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 212 | } else { |
| 213 | // Unlikely case - could be tuned. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 214 | RegStorage t_reg1 = AllocTemp(); |
| 215 | RegStorage t_reg2 = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 216 | LoadConstant(t_reg1, true_val); |
| 217 | LoadConstant(t_reg2, false_val); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 218 | OpRegImm(kOpCmp, rl_src.reg, 0); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 219 | LIR* it = OpIT(ccode, "E"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 220 | OpRegCopy(rl_result.reg, t_reg1); |
| 221 | OpRegCopy(rl_result.reg, t_reg2); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 222 | OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 223 | } |
| 224 | } else { |
| 225 | // MOVE case |
| 226 | RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]]; |
| 227 | RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]]; |
| 228 | rl_true = LoadValue(rl_true, kCoreReg); |
| 229 | rl_false = LoadValue(rl_false, kCoreReg); |
| 230 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 231 | OpRegImm(kOpCmp, rl_src.reg, 0); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 232 | LIR* it = nullptr; |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 233 | if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place? |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 234 | it = OpIT(NegateComparison(ccode), ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 235 | OpRegCopy(rl_result.reg, rl_false.reg); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 236 | } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place? |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 237 | it = OpIT(ccode, ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 238 | OpRegCopy(rl_result.reg, rl_true.reg); |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 239 | } else { // Normal - select between the two. |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 240 | it = OpIT(ccode, "E"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 241 | OpRegCopy(rl_result.reg, rl_true.reg); |
| 242 | OpRegCopy(rl_result.reg, rl_false.reg); |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 243 | } |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 244 | OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 245 | } |
| 246 | StoreValue(rl_dest, rl_result); |
| 247 | } |
| 248 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 249 | void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 250 | RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0); |
| 251 | RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2); |
| 252 | // Normalize such that if either operand is constant, src2 will be constant. |
Vladimir Marko | a894607 | 2014-01-22 10:30:44 +0000 | [diff] [blame] | 253 | ConditionCode ccode = mir->meta.ccode; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 254 | if (rl_src1.is_const) { |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 255 | std::swap(rl_src1, rl_src2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 256 | ccode = FlipComparisonOrder(ccode); |
| 257 | } |
| 258 | if (rl_src2.is_const) { |
| 259 | RegLocation rl_temp = UpdateLocWide(rl_src2); |
| 260 | // Do special compare/branch against simple const operand if not already in registers. |
| 261 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 262 | if ((rl_temp.location != kLocPhysReg) && |
| 263 | ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) { |
| 264 | GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode); |
| 265 | return; |
| 266 | } |
| 267 | } |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 268 | LIR* taken = &block_label_list_[bb->taken]; |
| 269 | LIR* not_taken = &block_label_list_[bb->fall_through]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 271 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 272 | OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh()); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 273 | switch (ccode) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 274 | case kCondEq: |
| 275 | OpCondBranch(kCondNe, not_taken); |
| 276 | break; |
| 277 | case kCondNe: |
| 278 | OpCondBranch(kCondNe, taken); |
| 279 | break; |
| 280 | case kCondLt: |
| 281 | OpCondBranch(kCondLt, taken); |
| 282 | OpCondBranch(kCondGt, not_taken); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 283 | ccode = kCondUlt; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | break; |
| 285 | case kCondLe: |
| 286 | OpCondBranch(kCondLt, taken); |
| 287 | OpCondBranch(kCondGt, not_taken); |
| 288 | ccode = kCondLs; |
| 289 | break; |
| 290 | case kCondGt: |
| 291 | OpCondBranch(kCondGt, taken); |
| 292 | OpCondBranch(kCondLt, not_taken); |
| 293 | ccode = kCondHi; |
| 294 | break; |
| 295 | case kCondGe: |
| 296 | OpCondBranch(kCondGt, taken); |
| 297 | OpCondBranch(kCondLt, not_taken); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 298 | ccode = kCondUge; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 299 | break; |
| 300 | default: |
| 301 | LOG(FATAL) << "Unexpected ccode: " << ccode; |
| 302 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 303 | OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 304 | OpCondBranch(ccode, taken); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Generate a register comparison to an immediate and branch. Caller |
| 309 | * is responsible for setting branch target field. |
| 310 | */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 311 | LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | LIR* branch; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 313 | ArmConditionCode arm_cond = ArmConditionEncoding(cond); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 314 | /* |
| 315 | * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit |
| 316 | * compare-and-branch if zero is ideal if it will reach. However, because null checks |
Mingyao Yang | 3a74d15 | 2014-04-21 15:39:44 -0700 | [diff] [blame^] | 317 | * branch forward to a slow path, they will frequently not reach - and thus have to |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 318 | * be converted to a long form during assembly (which will trigger another assembly |
| 319 | * pass). Here we estimate the branch distance for checks, and if large directly |
| 320 | * generate the long form in an attempt to avoid an extra assembly pass. |
Mingyao Yang | 3a74d15 | 2014-04-21 15:39:44 -0700 | [diff] [blame^] | 321 | * TODO: consider interspersing slowpaths in code following unconditional branches. |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 322 | */ |
| 323 | bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget)); |
| 324 | skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 325 | if (!skip && (ARM_LOWREG(reg.GetReg())) && (check_value == 0) && |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 326 | ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) { |
| 327 | branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz, |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 328 | reg.GetReg(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 329 | } else { |
Vladimir Marko | 2247984 | 2013-11-19 17:04:50 +0000 | [diff] [blame] | 330 | OpRegImm(kOpCmp, reg, check_value); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 331 | branch = NewLIR2(kThumbBCond, 0, arm_cond); |
| 332 | } |
| 333 | branch->target = target; |
| 334 | return branch; |
| 335 | } |
| 336 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 337 | LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 338 | LIR* res; |
| 339 | int opcode; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 340 | // If src or dest is a pair, we'll be using low reg. |
| 341 | if (r_dest.IsPair()) { |
| 342 | r_dest = r_dest.GetLow(); |
| 343 | } |
| 344 | if (r_src.IsPair()) { |
| 345 | r_src = r_src.GetLow(); |
| 346 | } |
| 347 | if (ARM_FPREG(r_dest.GetReg()) || ARM_FPREG(r_src.GetReg())) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 348 | return OpFpRegCopy(r_dest, r_src); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 349 | if (ARM_LOWREG(r_dest.GetReg()) && ARM_LOWREG(r_src.GetReg())) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 350 | opcode = kThumbMovRR; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 351 | else if (!ARM_LOWREG(r_dest.GetReg()) && !ARM_LOWREG(r_src.GetReg())) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 352 | opcode = kThumbMovRR_H2H; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 353 | else if (ARM_LOWREG(r_dest.GetReg())) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 354 | opcode = kThumbMovRR_H2L; |
| 355 | else |
| 356 | opcode = kThumbMovRR_L2H; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 357 | res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 358 | if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) { |
| 359 | res->flags.is_nop = true; |
| 360 | } |
| 361 | return res; |
| 362 | } |
| 363 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 364 | LIR* ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 365 | LIR* res = OpRegCopyNoInsert(r_dest, r_src); |
| 366 | AppendLIR(res); |
| 367 | return res; |
| 368 | } |
| 369 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 370 | void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) { |
| 371 | bool dest_fp = ARM_FPREG(r_dest.GetLowReg()); |
| 372 | bool src_fp = ARM_FPREG(r_src.GetLowReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 373 | if (dest_fp) { |
| 374 | if (src_fp) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 375 | // FIXME: handle 64-bit solo's here. |
| 376 | OpRegCopy(RegStorage::Solo64(S2d(r_dest.GetLowReg(), r_dest.GetHighReg())), |
| 377 | RegStorage::Solo64(S2d(r_src.GetLowReg(), r_src.GetHighReg()))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 378 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 379 | NewLIR3(kThumb2Fmdrr, S2d(r_dest.GetLowReg(), r_dest.GetHighReg()), |
| 380 | r_src.GetLowReg(), r_src.GetHighReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 381 | } |
| 382 | } else { |
| 383 | if (src_fp) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 384 | NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), S2d(r_src.GetLowReg(), |
| 385 | r_src.GetHighReg())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 386 | } else { |
| 387 | // Handle overlap |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 388 | if (r_src.GetHighReg() == r_dest.GetLowReg()) { |
| 389 | DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg()); |
| 390 | OpRegCopy(r_dest.GetHigh(), r_src.GetHigh()); |
| 391 | OpRegCopy(r_dest.GetLow(), r_src.GetLow()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 392 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 393 | OpRegCopy(r_dest.GetLow(), r_src.GetLow()); |
| 394 | OpRegCopy(r_dest.GetHigh(), r_src.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Table of magic divisors |
| 401 | struct MagicTable { |
| 402 | uint32_t magic; |
| 403 | uint32_t shift; |
| 404 | DividePattern pattern; |
| 405 | }; |
| 406 | |
| 407 | static const MagicTable magic_table[] = { |
| 408 | {0, 0, DivideNone}, // 0 |
| 409 | {0, 0, DivideNone}, // 1 |
| 410 | {0, 0, DivideNone}, // 2 |
| 411 | {0x55555556, 0, Divide3}, // 3 |
| 412 | {0, 0, DivideNone}, // 4 |
| 413 | {0x66666667, 1, Divide5}, // 5 |
| 414 | {0x2AAAAAAB, 0, Divide3}, // 6 |
| 415 | {0x92492493, 2, Divide7}, // 7 |
| 416 | {0, 0, DivideNone}, // 8 |
| 417 | {0x38E38E39, 1, Divide5}, // 9 |
| 418 | {0x66666667, 2, Divide5}, // 10 |
| 419 | {0x2E8BA2E9, 1, Divide5}, // 11 |
| 420 | {0x2AAAAAAB, 1, Divide5}, // 12 |
| 421 | {0x4EC4EC4F, 2, Divide5}, // 13 |
| 422 | {0x92492493, 3, Divide7}, // 14 |
| 423 | {0x88888889, 3, Divide7}, // 15 |
| 424 | }; |
| 425 | |
| 426 | // Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4) |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 427 | bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 428 | RegLocation rl_src, RegLocation rl_dest, int lit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 429 | if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) { |
| 430 | return false; |
| 431 | } |
| 432 | DividePattern pattern = magic_table[lit].pattern; |
| 433 | if (pattern == DivideNone) { |
| 434 | return false; |
| 435 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 436 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 437 | RegStorage r_magic = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 438 | LoadConstant(r_magic, magic_table[lit].magic); |
| 439 | rl_src = LoadValue(rl_src, kCoreReg); |
| 440 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 441 | RegStorage r_hi = AllocTemp(); |
| 442 | RegStorage r_lo = AllocTemp(); |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 443 | |
| 444 | // rl_dest and rl_src might overlap. |
| 445 | // Reuse r_hi to save the div result for reminder case. |
| 446 | RegStorage r_div_result = is_div ? rl_result.reg : r_hi; |
| 447 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 448 | NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg()); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 449 | switch (pattern) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 450 | case Divide3: |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 451 | OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | break; |
| 453 | case Divide5: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 454 | OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31); |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 455 | OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi, |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 456 | EncodeShift(kArmAsr, magic_table[lit].shift)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 457 | break; |
| 458 | case Divide7: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 459 | OpRegReg(kOpAdd, r_hi, rl_src.reg); |
| 460 | OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31); |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 461 | OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi, |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 462 | EncodeShift(kArmAsr, magic_table[lit].shift)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 463 | break; |
| 464 | default: |
| 465 | LOG(FATAL) << "Unexpected pattern: " << pattern; |
| 466 | } |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 467 | |
| 468 | if (!is_div) { |
| 469 | // div_result = src / lit |
| 470 | // tmp1 = div_result * lit |
| 471 | // dest = src - tmp1 |
| 472 | RegStorage tmp1 = r_lo; |
| 473 | EasyMultiplyOp ops[2]; |
| 474 | |
| 475 | bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops); |
| 476 | DCHECK_NE(canEasyMultiply, false); |
| 477 | |
| 478 | GenEasyMultiplyTwoOps(tmp1, r_div_result, ops); |
| 479 | OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1); |
| 480 | } |
| 481 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 482 | StoreValue(rl_dest, rl_result); |
| 483 | return true; |
| 484 | } |
| 485 | |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 486 | // Try to convert *lit to 1 RegRegRegShift/RegRegShift form. |
| 487 | bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) { |
| 488 | if (IsPowerOfTwo(lit)) { |
| 489 | op->op = kOpLsl; |
| 490 | op->shift = LowestSetBit(lit); |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | if (IsPowerOfTwo(lit - 1)) { |
| 495 | op->op = kOpAdd; |
| 496 | op->shift = LowestSetBit(lit - 1); |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | if (IsPowerOfTwo(lit + 1)) { |
| 501 | op->op = kOpRsub; |
| 502 | op->shift = LowestSetBit(lit + 1); |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | op->op = kOpInvalid; |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 507 | op->shift = 0; |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | |
| 511 | // Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms. |
| 512 | bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) { |
| 513 | GetEasyMultiplyOp(lit, &ops[0]); |
| 514 | if (GetEasyMultiplyOp(lit, &ops[0])) { |
| 515 | ops[1].op = kOpInvalid; |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 516 | ops[1].shift = 0; |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 517 | return true; |
| 518 | } |
| 519 | |
| 520 | int lit1 = lit; |
| 521 | uint32_t shift = LowestSetBit(lit1); |
| 522 | if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) { |
| 523 | ops[1].op = kOpLsl; |
| 524 | ops[1].shift = shift; |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | lit1 = lit - 1; |
| 529 | shift = LowestSetBit(lit1); |
| 530 | if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) { |
| 531 | ops[1].op = kOpAdd; |
| 532 | ops[1].shift = shift; |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | lit1 = lit + 1; |
| 537 | shift = LowestSetBit(lit1); |
| 538 | if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) { |
| 539 | ops[1].op = kOpRsub; |
| 540 | ops[1].shift = shift; |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 547 | // Generate instructions to do multiply. |
| 548 | // Additional temporary register is required, |
| 549 | // if it need to generate 2 instructions and src/dest overlap. |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 550 | void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) { |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 551 | // tmp1 = ( src << shift1) + [ src | -src | 0 ] |
| 552 | // dest = (tmp1 << shift2) + [ src | -src | 0 ] |
| 553 | |
| 554 | RegStorage r_tmp1; |
| 555 | if (ops[1].op == kOpInvalid) { |
| 556 | r_tmp1 = r_dest; |
| 557 | } else if (r_dest.GetReg() != r_src.GetReg()) { |
| 558 | r_tmp1 = r_dest; |
| 559 | } else { |
| 560 | r_tmp1 = AllocTemp(); |
| 561 | } |
| 562 | |
| 563 | switch (ops[0].op) { |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 564 | case kOpLsl: |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 565 | OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 566 | break; |
| 567 | case kOpAdd: |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 568 | OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift)); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 569 | break; |
| 570 | case kOpRsub: |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 571 | OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift)); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 572 | break; |
| 573 | default: |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 574 | DCHECK_EQ(ops[0].op, kOpInvalid); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 575 | break; |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | switch (ops[1].op) { |
| 579 | case kOpInvalid: |
| 580 | return; |
| 581 | case kOpLsl: |
| 582 | OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift); |
| 583 | break; |
| 584 | case kOpAdd: |
| 585 | OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift)); |
| 586 | break; |
| 587 | case kOpRsub: |
| 588 | OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift)); |
| 589 | break; |
| 590 | default: |
| 591 | LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps"; |
| 592 | break; |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) { |
| 597 | EasyMultiplyOp ops[2]; |
| 598 | |
| 599 | if (!GetEasyMultiplyTwoOps(lit, ops)) { |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | rl_src = LoadValue(rl_src, kCoreReg); |
| 604 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 605 | |
| 606 | GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops); |
| 607 | StoreValue(rl_dest, rl_result); |
| 608 | return true; |
| 609 | } |
| 610 | |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 611 | RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1, |
| 612 | RegLocation rl_src2, bool is_div, bool check_zero) { |
| 613 | LOG(FATAL) << "Unexpected use of GenDivRem for Arm"; |
| 614 | return rl_dest; |
| 615 | } |
| 616 | |
| 617 | RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) { |
| 618 | LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm"; |
| 619 | return rl_dest; |
| 620 | } |
| 621 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 622 | RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 623 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 624 | |
| 625 | // Put the literal in a temp. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 626 | RegStorage lit_temp = AllocTemp(); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 627 | LoadConstant(lit_temp, lit); |
| 628 | // Use the generic case for div/rem with arg2 in a register. |
| 629 | // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure. |
| 630 | rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div); |
| 631 | FreeTemp(lit_temp); |
| 632 | |
| 633 | return rl_result; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 634 | } |
| 635 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 636 | RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 637 | bool is_div) { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 638 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 639 | if (is_div) { |
| 640 | // Simple case, use sdiv instruction. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 641 | OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 642 | } else { |
| 643 | // Remainder case, use the following code: |
| 644 | // temp = reg1 / reg2 - integer division |
| 645 | // temp = temp * reg2 |
| 646 | // dest = reg1 - temp |
| 647 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 648 | RegStorage temp = AllocTemp(); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 649 | OpRegRegReg(kOpDiv, temp, reg1, reg2); |
| 650 | OpRegReg(kOpMul, temp, reg2); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 651 | OpRegRegReg(kOpSub, rl_result.reg, reg1, temp); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 652 | FreeTemp(temp); |
| 653 | } |
| 654 | |
| 655 | return rl_result; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 658 | bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 659 | DCHECK_EQ(cu_->instruction_set, kThumb2); |
| 660 | RegLocation rl_src1 = info->args[0]; |
| 661 | RegLocation rl_src2 = info->args[1]; |
| 662 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 663 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
| 664 | RegLocation rl_dest = InlineTarget(info); |
| 665 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 666 | OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 667 | LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 668 | OpRegReg(kOpMov, rl_result.reg, rl_src2.reg); |
| 669 | OpRegReg(kOpMov, rl_result.reg, rl_src1.reg); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 670 | OpEndIT(it); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 671 | StoreValue(rl_dest, rl_result); |
| 672 | return true; |
| 673 | } |
| 674 | |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 675 | bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) { |
| 676 | RegLocation rl_src_address = info->args[0]; // long address |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 677 | rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1] |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 678 | RegLocation rl_dest = InlineTarget(info); |
| 679 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); |
| 680 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 681 | if (size == kLong) { |
| 682 | // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 683 | if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) { |
| 684 | LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow()); |
| 685 | LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh()); |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 686 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 687 | LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh()); |
| 688 | LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow()); |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 689 | } |
| 690 | StoreValueWide(rl_dest, rl_result); |
| 691 | } else { |
| 692 | DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord); |
| 693 | // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 694 | LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, INVALID_SREG); |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 695 | StoreValue(rl_dest, rl_result); |
| 696 | } |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) { |
| 701 | RegLocation rl_src_address = info->args[0]; // long address |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 702 | rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1] |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 703 | RegLocation rl_src_value = info->args[2]; // [size] value |
| 704 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); |
| 705 | if (size == kLong) { |
| 706 | // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0. |
| 707 | RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 708 | StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), kWord); |
| 709 | StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), kWord); |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 710 | } else { |
| 711 | DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord); |
| 712 | // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0. |
| 713 | RegLocation rl_value = LoadValue(rl_src_value, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 714 | StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size); |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 715 | } |
| 716 | return true; |
| 717 | } |
| 718 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 719 | void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 720 | LOG(FATAL) << "Unexpected use of OpLea for Arm"; |
| 721 | } |
| 722 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 723 | void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 724 | LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm"; |
| 725 | } |
| 726 | |
Vladimir Marko | 1c282e2 | 2013-11-21 14:49:47 +0000 | [diff] [blame] | 727 | bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 728 | DCHECK_EQ(cu_->instruction_set, kThumb2); |
| 729 | // Unused - RegLocation rl_src_unsafe = info->args[0]; |
Vladimir Marko | 1c282e2 | 2013-11-21 14:49:47 +0000 | [diff] [blame] | 730 | RegLocation rl_src_obj = info->args[1]; // Object - known non-null |
| 731 | RegLocation rl_src_offset = info->args[2]; // long low |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 732 | rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3] |
Vladimir Marko | 1c282e2 | 2013-11-21 14:49:47 +0000 | [diff] [blame] | 733 | RegLocation rl_src_expected = info->args[4]; // int, long or Object |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 734 | // If is_long, high half is in info->args[5] |
| 735 | RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object |
| 736 | // If is_long, high half is in info->args[7] |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 737 | RegLocation rl_dest = InlineTarget(info); // boolean place for result |
| 738 | |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 739 | // We have only 5 temporary registers available and actually only 4 if the InlineTarget |
| 740 | // above locked one of the temps. For a straightforward CAS64 we need 7 registers: |
| 741 | // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor |
| 742 | // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop |
| 743 | // into the same temps, reducing the number of required temps down to 5. We shall work |
| 744 | // around the potentially locked temp by using LR for r_ptr, unconditionally. |
| 745 | // TODO: Pass information about the need for more temps to the stack frame generation |
| 746 | // code so that we can rely on being able to allocate enough temps. |
| 747 | DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp); |
| 748 | MarkTemp(rARM_LR); |
| 749 | FreeTemp(rARM_LR); |
| 750 | LockTemp(rARM_LR); |
| 751 | bool load_early = true; |
| 752 | if (is_long) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 753 | int expected_reg = is_long ? rl_src_expected.reg.GetLowReg() : rl_src_expected.reg.GetReg(); |
| 754 | int new_val_reg = is_long ? rl_src_new_value.reg.GetLowReg() : rl_src_new_value.reg.GetReg(); |
| 755 | bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !IsFpReg(expected_reg); |
| 756 | bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !IsFpReg(new_val_reg); |
| 757 | bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg); |
| 758 | bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 759 | |
| 760 | if (!expected_is_good_reg && !new_value_is_good_reg) { |
| 761 | // None of expected/new_value is non-temp reg, need to load both late |
| 762 | load_early = false; |
| 763 | // Make sure they are not in the temp regs and the load will not be skipped. |
| 764 | if (expected_is_core_reg) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 765 | FlushRegWide(rl_src_expected.reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 766 | ClobberSReg(rl_src_expected.s_reg_low); |
| 767 | ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low)); |
| 768 | rl_src_expected.location = kLocDalvikFrame; |
| 769 | } |
| 770 | if (new_value_is_core_reg) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 771 | FlushRegWide(rl_src_new_value.reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 772 | ClobberSReg(rl_src_new_value.s_reg_low); |
| 773 | ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low)); |
| 774 | rl_src_new_value.location = kLocDalvikFrame; |
| 775 | } |
| 776 | } |
| 777 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 778 | |
| 779 | // Release store semantics, get the barrier out of the way. TODO: revisit |
| 780 | GenMemBarrier(kStoreLoad); |
| 781 | |
| 782 | RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 783 | RegLocation rl_new_value; |
| 784 | if (!is_long) { |
| 785 | rl_new_value = LoadValue(rl_src_new_value, kCoreReg); |
| 786 | } else if (load_early) { |
| 787 | rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg); |
| 788 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 789 | |
Vladimir Marko | 1c282e2 | 2013-11-21 14:49:47 +0000 | [diff] [blame] | 790 | if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 791 | // Mark card for object assuming new value is stored. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 792 | MarkGCCard(rl_new_value.reg, rl_object.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg); |
| 796 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 797 | RegStorage r_ptr = rs_rARM_LR; |
| 798 | OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 799 | |
| 800 | // Free now unneeded rl_object and rl_offset to give more temps. |
| 801 | ClobberSReg(rl_object.s_reg_low); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 802 | FreeTemp(rl_object.reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 803 | ClobberSReg(rl_offset.s_reg_low); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 804 | FreeTemp(rl_offset.reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 805 | |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 806 | RegLocation rl_expected; |
| 807 | if (!is_long) { |
| 808 | rl_expected = LoadValue(rl_src_expected, kCoreReg); |
| 809 | } else if (load_early) { |
| 810 | rl_expected = LoadValueWide(rl_src_expected, kCoreReg); |
| 811 | } else { |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 812 | // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 813 | int low_reg = AllocTemp().GetReg(); |
| 814 | int high_reg = AllocTemp().GetReg(); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 815 | rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg); |
| 816 | rl_expected = rl_new_value; |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 817 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 818 | |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 819 | // do { |
| 820 | // tmp = [r_ptr] - expected; |
| 821 | // } while (tmp == 0 && failure([r_ptr] <- r_new_value)); |
| 822 | // result = tmp != 0; |
| 823 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 824 | RegStorage r_tmp = AllocTemp(); |
Jeff Hao | 2de2aa1 | 2013-09-12 17:20:31 -0700 | [diff] [blame] | 825 | LIR* target = NewLIR0(kPseudoTargetLabel); |
Jeff Hao | 2de2aa1 | 2013-09-12 17:20:31 -0700 | [diff] [blame] | 826 | |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 827 | LIR* it = nullptr; |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 828 | if (is_long) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 829 | RegStorage r_tmp_high = AllocTemp(); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 830 | if (!load_early) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 831 | LoadValueDirectWide(rl_src_expected, rl_expected.reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 832 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 833 | NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg()); |
| 834 | OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow()); |
| 835 | OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh()); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 836 | if (!load_early) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 837 | LoadValueDirectWide(rl_src_new_value, rl_new_value.reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 838 | } |
| 839 | // Make sure we use ORR that sets the ccode |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 840 | if (ARM_LOWREG(r_tmp.GetReg()) && ARM_LOWREG(r_tmp_high.GetReg())) { |
| 841 | NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg()); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 842 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 843 | NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 844 | } |
| 845 | FreeTemp(r_tmp_high); // Now unneeded |
| 846 | |
| 847 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 848 | it = OpIT(kCondEq, "T"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 849 | NewLIR4(kThumb2Strexd /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetLowReg(), rl_new_value.reg.GetHighReg(), r_ptr.GetReg()); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 850 | |
| 851 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 852 | NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0); |
| 853 | OpRegReg(kOpSub, r_tmp, rl_expected.reg); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 854 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 855 | it = OpIT(kCondEq, "T"); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 856 | NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | // Still one conditional left from OpIT(kCondEq, "T") from either branch |
| 860 | OpRegImm(kOpCmp /* eq */, r_tmp, 1); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 861 | OpEndIT(it); |
Dave Allison | 43a065c | 2014-04-01 15:14:46 -0700 | [diff] [blame] | 862 | |
Jeff Hao | 2de2aa1 | 2013-09-12 17:20:31 -0700 | [diff] [blame] | 863 | OpCondBranch(kCondEq, target); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 864 | |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 865 | if (!load_early) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 866 | FreeTemp(rl_expected.reg); // Now unneeded. |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | // result := (tmp1 != 0) ? 0 : 1; |
| 870 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 871 | OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1); |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 872 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 873 | it = OpIT(kCondUlt, ""); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 874 | LoadConstant(rl_result.reg, 0); /* cc */ |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 875 | FreeTemp(r_tmp); // Now unneeded. |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 876 | OpEndIT(it); // Barrier to terminate OpIT. |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 877 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 878 | StoreValue(rl_dest, rl_result); |
| 879 | |
Vladimir Marko | 3e5af82 | 2013-11-21 15:01:20 +0000 | [diff] [blame] | 880 | // Now, restore lr to its non-temp status. |
| 881 | Clobber(rARM_LR); |
| 882 | UnmarkTemp(rARM_LR); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 883 | return true; |
| 884 | } |
| 885 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 886 | LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) { |
| 887 | return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 888 | } |
| 889 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 890 | LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) { |
| 891 | return NewLIR3(kThumb2Vldms, r_base.GetReg(), fr0, count); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 892 | } |
| 893 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 894 | LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) { |
| 895 | return NewLIR3(kThumb2Vstms, r_base.GetReg(), fr0, count); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src, |
| 899 | RegLocation rl_result, int lit, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 900 | int first_bit, int second_bit) { |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 901 | OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 902 | EncodeShift(kArmLsl, second_bit - first_bit)); |
| 903 | if (first_bit != 0) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 904 | OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 908 | void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 909 | DCHECK(reg.IsPair()); // TODO: support k64BitSolo. |
| 910 | RegStorage t_reg = AllocTemp(); |
| 911 | NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 912 | FreeTemp(t_reg); |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 913 | GenDivZeroCheck(kCondEq); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Test suspend flag, return target of taken suspend branch |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 917 | LIR* ArmMir2Lir::OpTestSuspend(LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 918 | NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1); |
| 919 | return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target); |
| 920 | } |
| 921 | |
| 922 | // Decrement register and branch on condition |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 923 | LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 924 | // Combine sub & test using sub setflags encoding here |
Vladimir Marko | dbb8c49 | 2014-02-28 17:36:39 +0000 | [diff] [blame] | 925 | OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags. |
| 926 | DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 927 | return OpCondBranch(c_code, target); |
| 928 | } |
| 929 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 930 | void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 931 | #if ANDROID_SMP != 0 |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 932 | // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one. |
| 933 | LIR* barrier = last_lir_insn_; |
| 934 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 935 | int dmb_flavor; |
| 936 | // TODO: revisit Arm barrier kinds |
| 937 | switch (barrier_kind) { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 938 | case kLoadStore: dmb_flavor = kISH; break; |
| 939 | case kLoadLoad: dmb_flavor = kISH; break; |
| 940 | case kStoreStore: dmb_flavor = kISHST; break; |
| 941 | case kStoreLoad: dmb_flavor = kISH; break; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 942 | default: |
| 943 | LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind; |
| 944 | dmb_flavor = kSY; // quiet gcc. |
| 945 | break; |
| 946 | } |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 947 | |
| 948 | // If the same barrier already exists, don't generate another. |
| 949 | if (barrier == nullptr |
| 950 | || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) { |
| 951 | barrier = NewLIR1(kThumb2Dmb, dmb_flavor); |
| 952 | } |
| 953 | |
| 954 | // At this point we must have a memory barrier. Mark it as a scheduling barrier as well. |
| 955 | DCHECK(!barrier->flags.use_def_invalid); |
| 956 | barrier->u.m.def_mask = ENCODE_ALL; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 957 | #endif |
| 958 | } |
| 959 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 960 | void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 961 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 962 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 963 | RegStorage z_reg = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 964 | LoadConstantNoClobber(z_reg, 0); |
| 965 | // Check for destructive overlap |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 966 | if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) { |
| 967 | RegStorage t_reg = AllocTemp(); |
| 968 | OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow()); |
| 969 | OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 970 | FreeTemp(t_reg); |
| 971 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 972 | OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow()); |
| 973 | OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 974 | } |
| 975 | FreeTemp(z_reg); |
| 976 | StoreValueWide(rl_dest, rl_result); |
| 977 | } |
| 978 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 979 | void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest, |
| 980 | RegLocation rl_src1, RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 981 | /* |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 982 | * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed |
| 983 | * dest = src1.lo * src2.lo; |
| 984 | * tmp1 += src1.lo * src2.hi; |
| 985 | * dest.hi += tmp1; |
| 986 | * |
| 987 | * To pull off inline multiply, we have a worst-case requirement of 7 temporary |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 988 | * registers. Normally for Arm, we get 5. We can get to 6 by including |
| 989 | * lr in the temp set. The only problematic case is all operands and result are |
| 990 | * distinct, and none have been promoted. In that case, we can succeed by aggressively |
| 991 | * freeing operand temp registers after they are no longer needed. All other cases |
| 992 | * can proceed normally. We'll just punt on the case of the result having a misaligned |
| 993 | * overlap with either operand and send that case to a runtime handler. |
| 994 | */ |
| 995 | RegLocation rl_result; |
| 996 | if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 997 | ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 998 | FlushAllRegs(); |
| 999 | CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false); |
| 1000 | rl_result = GetReturnWide(false); |
| 1001 | StoreValueWide(rl_dest, rl_result); |
| 1002 | return; |
| 1003 | } |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1004 | |
| 1005 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1006 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 1007 | |
| 1008 | int reg_status = 0; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1009 | RegStorage res_lo; |
| 1010 | RegStorage res_hi; |
| 1011 | bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() && |
| 1012 | !IsTemp(rl_dest.reg.GetLowReg()) && !IsTemp(rl_dest.reg.GetHighReg()); |
| 1013 | bool src1_promoted = !IsTemp(rl_src1.reg.GetLowReg()) && !IsTemp(rl_src1.reg.GetHighReg()); |
| 1014 | bool src2_promoted = !IsTemp(rl_src2.reg.GetLowReg()) && !IsTemp(rl_src2.reg.GetHighReg()); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1015 | // Check if rl_dest is *not* either operand and we have enough temp registers. |
| 1016 | if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) && |
| 1017 | (dest_promoted || src1_promoted || src2_promoted)) { |
| 1018 | // In this case, we do not need to manually allocate temp registers for result. |
| 1019 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1020 | res_lo = rl_result.reg.GetLow(); |
| 1021 | res_hi = rl_result.reg.GetHigh(); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1022 | } else { |
| 1023 | res_lo = AllocTemp(); |
| 1024 | if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) { |
| 1025 | // In this case, we have enough temp registers to be allocated for result. |
| 1026 | res_hi = AllocTemp(); |
| 1027 | reg_status = 1; |
| 1028 | } else { |
| 1029 | // In this case, all temps are now allocated. |
| 1030 | // res_hi will be allocated after we can free src1_hi. |
| 1031 | reg_status = 2; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1035 | // Temporarily add LR to the temp pool, and assign it to tmp1 |
| 1036 | MarkTemp(rARM_LR); |
| 1037 | FreeTemp(rARM_LR); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1038 | RegStorage tmp1 = rs_rARM_LR; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1039 | LockTemp(rARM_LR); |
| 1040 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1041 | if (rl_src1.reg == rl_src2.reg) { |
| 1042 | DCHECK(res_hi.Valid()); |
| 1043 | DCHECK(res_lo.Valid()); |
| 1044 | NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg()); |
| 1045 | NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(), |
| 1046 | rl_src1.reg.GetLowReg()); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1047 | OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1048 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1049 | NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg()); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1050 | if (reg_status == 2) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1051 | DCHECK(!res_hi.Valid()); |
| 1052 | DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg()); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1053 | DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg()); |
| 1054 | FreeTemp(rl_src1.reg.GetHighReg()); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1055 | res_hi = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1056 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1057 | DCHECK(res_hi.Valid()); |
| 1058 | DCHECK(res_lo.Valid()); |
| 1059 | NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(), |
| 1060 | rl_src1.reg.GetLowReg()); |
| 1061 | NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(), |
| 1062 | tmp1.GetReg()); |
| 1063 | NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1064 | if (reg_status == 2) { |
| 1065 | // Clobber rl_src1 since it was corrupted. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1066 | FreeTemp(rl_src1.reg); |
| 1067 | Clobber(rl_src1.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1070 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1071 | // Now, restore lr to its non-temp status. |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1072 | FreeTemp(tmp1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1073 | Clobber(rARM_LR); |
| 1074 | UnmarkTemp(rARM_LR); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (reg_status != 0) { |
| 1077 | // We had manually allocated registers for rl_result. |
| 1078 | // Now construct a RegLocation. |
| 1079 | rl_result = GetReturnWide(false); // Just using as a template. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1080 | rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi); |
Zheng Xu | d7f8e02 | 2014-03-13 13:40:30 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | StoreValueWide(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1086 | void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1087 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1088 | LOG(FATAL) << "Unexpected use of GenAddLong for Arm"; |
| 1089 | } |
| 1090 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1091 | void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1092 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1093 | LOG(FATAL) << "Unexpected use of GenSubLong for Arm"; |
| 1094 | } |
| 1095 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1096 | void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1097 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1098 | LOG(FATAL) << "Unexpected use of GenAndLong for Arm"; |
| 1099 | } |
| 1100 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1101 | void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1102 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1103 | LOG(FATAL) << "Unexpected use of GenOrLong for Arm"; |
| 1104 | } |
| 1105 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1106 | void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1107 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1108 | LOG(FATAL) << "Unexpected use of genXoLong for Arm"; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Generate array load |
| 1113 | */ |
| 1114 | void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1115 | RegLocation rl_index, RegLocation rl_dest, int scale) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1116 | RegisterClass reg_class = oat_reg_class_by_size(size); |
| 1117 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
| 1118 | int data_offset; |
| 1119 | RegLocation rl_result; |
| 1120 | bool constant_index = rl_index.is_const; |
| 1121 | rl_array = LoadValue(rl_array, kCoreReg); |
| 1122 | if (!constant_index) { |
| 1123 | rl_index = LoadValue(rl_index, kCoreReg); |
| 1124 | } |
| 1125 | |
| 1126 | if (rl_dest.wide) { |
| 1127 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1128 | } else { |
| 1129 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1130 | } |
| 1131 | |
| 1132 | // If index is constant, just fold it into the data offset |
| 1133 | if (constant_index) { |
| 1134 | data_offset += mir_graph_->ConstantValue(rl_index) << scale; |
| 1135 | } |
| 1136 | |
| 1137 | /* null object? */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1138 | GenNullCheck(rl_array.reg, opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1139 | |
| 1140 | bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK)); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1141 | RegStorage reg_len; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1142 | if (needs_range_check) { |
| 1143 | reg_len = AllocTemp(); |
| 1144 | /* Get len */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1145 | LoadWordDisp(rl_array.reg, len_offset, reg_len); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1146 | MarkPossibleNullPointerException(opt_flags); |
| 1147 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1148 | ForceImplicitNullCheck(rl_array.reg, opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1149 | } |
| 1150 | if (rl_dest.wide || rl_dest.fp || constant_index) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1151 | RegStorage reg_ptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1152 | if (constant_index) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1153 | reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | // No special indexed operation, lea + load w/ displacement |
| 1156 | reg_ptr = AllocTemp(); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1157 | OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale)); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1158 | FreeTemp(rl_index.reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1159 | } |
| 1160 | rl_result = EvalLoc(rl_dest, reg_class, true); |
| 1161 | |
| 1162 | if (needs_range_check) { |
| 1163 | if (constant_index) { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1164 | GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1165 | } else { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1166 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1167 | } |
| 1168 | FreeTemp(reg_len); |
| 1169 | } |
| 1170 | if (rl_dest.wide) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1171 | LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg, INVALID_SREG); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1172 | MarkPossibleNullPointerException(opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1173 | if (!constant_index) { |
| 1174 | FreeTemp(reg_ptr); |
| 1175 | } |
| 1176 | StoreValueWide(rl_dest, rl_result); |
| 1177 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1178 | LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, INVALID_SREG); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1179 | MarkPossibleNullPointerException(opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1180 | if (!constant_index) { |
| 1181 | FreeTemp(reg_ptr); |
| 1182 | } |
| 1183 | StoreValue(rl_dest, rl_result); |
| 1184 | } |
| 1185 | } else { |
| 1186 | // Offset base, then use indexed load |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1187 | RegStorage reg_ptr = AllocTemp(); |
| 1188 | OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1189 | FreeTemp(rl_array.reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1190 | rl_result = EvalLoc(rl_dest, reg_class, true); |
| 1191 | |
| 1192 | if (needs_range_check) { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1193 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1194 | FreeTemp(reg_len); |
| 1195 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1196 | LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1197 | MarkPossibleNullPointerException(opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1198 | FreeTemp(reg_ptr); |
| 1199 | StoreValue(rl_dest, rl_result); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * Generate array store |
| 1205 | * |
| 1206 | */ |
| 1207 | void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array, |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1208 | RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1209 | RegisterClass reg_class = oat_reg_class_by_size(size); |
| 1210 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1211 | bool constant_index = rl_index.is_const; |
| 1212 | |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1213 | int data_offset; |
| 1214 | if (size == kLong || size == kDouble) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1215 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1216 | } else { |
| 1217 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1218 | } |
| 1219 | |
| 1220 | // If index is constant, just fold it into the data offset. |
| 1221 | if (constant_index) { |
| 1222 | data_offset += mir_graph_->ConstantValue(rl_index) << scale; |
| 1223 | } |
| 1224 | |
| 1225 | rl_array = LoadValue(rl_array, kCoreReg); |
| 1226 | if (!constant_index) { |
| 1227 | rl_index = LoadValue(rl_index, kCoreReg); |
| 1228 | } |
| 1229 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1230 | RegStorage reg_ptr; |
Ian Rogers | 773aab1 | 2013-10-14 13:50:10 -0700 | [diff] [blame] | 1231 | bool allocated_reg_ptr_temp = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1232 | if (constant_index) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1233 | reg_ptr = rl_array.reg; |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1234 | } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) { |
| 1235 | Clobber(rl_array.reg.GetReg()); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1236 | reg_ptr = rl_array.reg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1237 | } else { |
Ian Rogers | 773aab1 | 2013-10-14 13:50:10 -0700 | [diff] [blame] | 1238 | allocated_reg_ptr_temp = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1239 | reg_ptr = AllocTemp(); |
| 1240 | } |
| 1241 | |
| 1242 | /* null object? */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1243 | GenNullCheck(rl_array.reg, opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1244 | |
| 1245 | bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK)); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1246 | RegStorage reg_len; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1247 | if (needs_range_check) { |
| 1248 | reg_len = AllocTemp(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1249 | // NOTE: max live temps(4) here. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1250 | /* Get len */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1251 | LoadWordDisp(rl_array.reg, len_offset, reg_len); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1252 | MarkPossibleNullPointerException(opt_flags); |
| 1253 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1254 | ForceImplicitNullCheck(rl_array.reg, opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1255 | } |
| 1256 | /* at this point, reg_ptr points to array, 2 live temps */ |
| 1257 | if (rl_src.wide || rl_src.fp || constant_index) { |
| 1258 | if (rl_src.wide) { |
| 1259 | rl_src = LoadValueWide(rl_src, reg_class); |
| 1260 | } else { |
| 1261 | rl_src = LoadValue(rl_src, reg_class); |
| 1262 | } |
| 1263 | if (!constant_index) { |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1264 | OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1265 | } |
| 1266 | if (needs_range_check) { |
| 1267 | if (constant_index) { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1268 | GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1269 | } else { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1270 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1271 | } |
| 1272 | FreeTemp(reg_len); |
| 1273 | } |
| 1274 | |
| 1275 | if (rl_src.wide) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1276 | StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1277 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1278 | StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1279 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1280 | MarkPossibleNullPointerException(opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1281 | } else { |
| 1282 | /* reg_ptr -> array data */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1283 | OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1284 | rl_src = LoadValue(rl_src, reg_class); |
| 1285 | if (needs_range_check) { |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 1286 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1287 | FreeTemp(reg_len); |
| 1288 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1289 | StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1290 | MarkPossibleNullPointerException(opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1291 | } |
Ian Rogers | 773aab1 | 2013-10-14 13:50:10 -0700 | [diff] [blame] | 1292 | if (allocated_reg_ptr_temp) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1293 | FreeTemp(reg_ptr); |
| 1294 | } |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1295 | if (card_mark) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1296 | MarkGCCard(rl_src.reg, rl_array.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1300 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1301 | void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1302 | RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1303 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 1304 | // Per spec, we only care about low 6 bits of shift amount. |
| 1305 | int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f; |
| 1306 | if (shift_amount == 0) { |
| 1307 | StoreValueWide(rl_dest, rl_src); |
| 1308 | return; |
| 1309 | } |
| 1310 | if (BadOverlap(rl_src, rl_dest)) { |
| 1311 | GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift); |
| 1312 | return; |
| 1313 | } |
| 1314 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1315 | switch (opcode) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1316 | case Instruction::SHL_LONG: |
| 1317 | case Instruction::SHL_LONG_2ADDR: |
| 1318 | if (shift_amount == 1) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1319 | OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow()); |
| 1320 | OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1321 | } else if (shift_amount == 32) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1322 | OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg); |
| 1323 | LoadConstant(rl_result.reg.GetLow(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1324 | } else if (shift_amount > 31) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1325 | OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32); |
| 1326 | LoadConstant(rl_result.reg.GetLow(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1327 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1328 | OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1329 | OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1330 | EncodeShift(kArmLsr, 32 - shift_amount)); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1331 | OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1332 | } |
| 1333 | break; |
| 1334 | case Instruction::SHR_LONG: |
| 1335 | case Instruction::SHR_LONG_2ADDR: |
| 1336 | if (shift_amount == 32) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1337 | OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh()); |
| 1338 | OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1339 | } else if (shift_amount > 31) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1340 | OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32); |
| 1341 | OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1342 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1343 | RegStorage t_reg = AllocTemp(); |
| 1344 | OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1345 | OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1346 | EncodeShift(kArmLsl, 32 - shift_amount)); |
| 1347 | FreeTemp(t_reg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1348 | OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1349 | } |
| 1350 | break; |
| 1351 | case Instruction::USHR_LONG: |
| 1352 | case Instruction::USHR_LONG_2ADDR: |
| 1353 | if (shift_amount == 32) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1354 | OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh()); |
| 1355 | LoadConstant(rl_result.reg.GetHigh(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1356 | } else if (shift_amount > 31) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1357 | OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32); |
| 1358 | LoadConstant(rl_result.reg.GetHigh(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1359 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1360 | RegStorage t_reg = AllocTemp(); |
| 1361 | OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount); |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1362 | OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1363 | EncodeShift(kArmLsl, 32 - shift_amount)); |
| 1364 | FreeTemp(t_reg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1365 | OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1366 | } |
| 1367 | break; |
| 1368 | default: |
| 1369 | LOG(FATAL) << "Unexpected case"; |
| 1370 | } |
| 1371 | StoreValueWide(rl_dest, rl_result); |
| 1372 | } |
| 1373 | |
| 1374 | void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1375 | RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1376 | if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) { |
| 1377 | if (!rl_src2.is_const) { |
| 1378 | // Don't bother with special handling for subtract from immediate. |
| 1379 | GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2); |
| 1380 | return; |
| 1381 | } |
| 1382 | } else { |
| 1383 | // Normalize |
| 1384 | if (!rl_src2.is_const) { |
| 1385 | DCHECK(rl_src1.is_const); |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 1386 | std::swap(rl_src1, rl_src2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | if (BadOverlap(rl_src1, rl_dest)) { |
| 1390 | GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2); |
| 1391 | return; |
| 1392 | } |
| 1393 | DCHECK(rl_src2.is_const); |
| 1394 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 1395 | uint32_t val_lo = Low32Bits(val); |
| 1396 | uint32_t val_hi = High32Bits(val); |
| 1397 | int32_t mod_imm_lo = ModifiedImmediate(val_lo); |
| 1398 | int32_t mod_imm_hi = ModifiedImmediate(val_hi); |
| 1399 | |
| 1400 | // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1401 | switch (opcode) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1402 | case Instruction::ADD_LONG: |
| 1403 | case Instruction::ADD_LONG_2ADDR: |
| 1404 | case Instruction::SUB_LONG: |
| 1405 | case Instruction::SUB_LONG_2ADDR: |
| 1406 | if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) { |
| 1407 | GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2); |
| 1408 | return; |
| 1409 | } |
| 1410 | break; |
| 1411 | default: |
| 1412 | break; |
| 1413 | } |
| 1414 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1415 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1416 | // NOTE: once we've done the EvalLoc on dest, we can no longer bail. |
| 1417 | switch (opcode) { |
| 1418 | case Instruction::ADD_LONG: |
| 1419 | case Instruction::ADD_LONG_2ADDR: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1420 | NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1421 | NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1422 | break; |
| 1423 | case Instruction::OR_LONG: |
| 1424 | case Instruction::OR_LONG_2ADDR: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1425 | if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) { |
| 1426 | OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1427 | } |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1428 | if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1429 | OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1430 | } |
| 1431 | break; |
| 1432 | case Instruction::XOR_LONG: |
| 1433 | case Instruction::XOR_LONG_2ADDR: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1434 | OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo); |
| 1435 | OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1436 | break; |
| 1437 | case Instruction::AND_LONG: |
| 1438 | case Instruction::AND_LONG_2ADDR: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1439 | if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) { |
| 1440 | OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1441 | } |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1442 | if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1443 | OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1444 | } |
| 1445 | break; |
| 1446 | case Instruction::SUB_LONG_2ADDR: |
| 1447 | case Instruction::SUB_LONG: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1448 | NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 1449 | NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1450 | break; |
| 1451 | default: |
| 1452 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 1453 | } |
| 1454 | StoreValueWide(rl_dest, rl_result); |
| 1455 | } |
| 1456 | |
| 1457 | } // namespace art |