Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [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 "arm64_lir.h" |
| 20 | #include "codegen_arm64.h" |
| 21 | #include "dex/quick/mir_to_lir-inl.h" |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame^] | 22 | #include "dex/reg_storage_eq.h" |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints.h" |
| 24 | #include "mirror/array.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | LIR* Arm64Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) { |
| 29 | OpRegReg(kOpCmp, src1, src2); |
| 30 | return OpCondBranch(cond, target); |
| 31 | } |
| 32 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 33 | LIR* Arm64Mir2Lir::OpIT(ConditionCode ccode, const char* guide) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 34 | LOG(FATAL) << "Unexpected use of OpIT for Arm64"; |
| 35 | return NULL; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void Arm64Mir2Lir::OpEndIT(LIR* it) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 39 | LOG(FATAL) << "Unexpected use of OpEndIT for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * 64-bit 3way compare function. |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 44 | * cmp xA, xB |
Zheng Xu | 511c8a6 | 2014-06-03 16:22:23 +0800 | [diff] [blame] | 45 | * csinc wC, wzr, wzr, eq // wC = (xA == xB) ? 0 : 1 |
| 46 | * csneg wC, wC, wC, ge // wC = (xA >= xB) ? wC : -wC |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 47 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 48 | void Arm64Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, |
| 49 | RegLocation rl_src2) { |
| 50 | RegLocation rl_result; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 51 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 52 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 53 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 54 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 55 | OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg); |
Zheng Xu | 511c8a6 | 2014-06-03 16:22:23 +0800 | [diff] [blame] | 56 | NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondEq); |
| 57 | NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_result.reg.GetReg(), |
| 58 | rl_result.reg.GetReg(), kArmCondGe); |
| 59 | StoreValue(rl_dest, rl_result); |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void Arm64Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest, |
| 63 | RegLocation rl_src1, RegLocation rl_shift) { |
| 64 | OpKind op = kOpBkpt; |
| 65 | switch (opcode) { |
| 66 | case Instruction::SHL_LONG: |
| 67 | case Instruction::SHL_LONG_2ADDR: |
| 68 | op = kOpLsl; |
| 69 | break; |
| 70 | case Instruction::SHR_LONG: |
| 71 | case Instruction::SHR_LONG_2ADDR: |
| 72 | op = kOpAsr; |
| 73 | break; |
| 74 | case Instruction::USHR_LONG: |
| 75 | case Instruction::USHR_LONG_2ADDR: |
| 76 | op = kOpLsr; |
| 77 | break; |
| 78 | default: |
| 79 | LOG(FATAL) << "Unexpected case: " << opcode; |
| 80 | } |
Zheng Xu | e2eb29e | 2014-06-12 10:22:33 +0800 | [diff] [blame] | 81 | rl_shift = LoadValue(rl_shift, kCoreReg); |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 82 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 83 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
Zheng Xu | e2eb29e | 2014-06-12 10:22:33 +0800 | [diff] [blame] | 84 | OpRegRegReg(op, rl_result.reg, rl_src1.reg, As64BitReg(rl_shift.reg)); |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 85 | StoreValueWide(rl_dest, rl_result); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 88 | void Arm64Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) { |
| 89 | RegLocation rl_result; |
| 90 | RegLocation rl_src = mir_graph_->GetSrc(mir, 0); |
| 91 | RegLocation rl_dest = mir_graph_->GetDest(mir); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 92 | RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg; |
| 93 | RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg; |
| 94 | rl_src = LoadValue(rl_src, src_reg_class); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 95 | ArmConditionCode code = ArmConditionEncoding(mir->meta.ccode); |
| 96 | |
| 97 | RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]]; |
| 98 | RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]]; |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 99 | rl_true = LoadValue(rl_true, result_reg_class); |
| 100 | rl_false = LoadValue(rl_false, result_reg_class); |
| 101 | rl_result = EvalLoc(rl_dest, result_reg_class, true); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 102 | OpRegImm(kOpCmp, rl_src.reg, 0); |
| 103 | NewLIR4(kA64Csel4rrrc, rl_result.reg.GetReg(), rl_true.reg.GetReg(), |
| 104 | rl_false.reg.GetReg(), code); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 105 | StoreValue(rl_dest, rl_result); |
| 106 | } |
| 107 | |
| 108 | void Arm64Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) { |
| 109 | RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0); |
| 110 | RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 111 | LIR* taken = &block_label_list_[bb->taken]; |
| 112 | LIR* not_taken = &block_label_list_[bb->fall_through]; |
| 113 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 114 | // Normalize such that if either operand is constant, src2 will be constant. |
| 115 | ConditionCode ccode = mir->meta.ccode; |
| 116 | if (rl_src1.is_const) { |
| 117 | std::swap(rl_src1, rl_src2); |
| 118 | ccode = FlipComparisonOrder(ccode); |
| 119 | } |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 120 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 121 | if (rl_src2.is_const) { |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 122 | rl_src2 = UpdateLocWide(rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 123 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 124 | // Special handling using cbz & cbnz. |
| 125 | if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) { |
| 126 | OpCmpImmBranch(ccode, rl_src1.reg, 0, taken); |
| 127 | OpCmpImmBranch(NegateComparison(ccode), rl_src1.reg, 0, not_taken); |
| 128 | return; |
| 129 | // Only handle Imm if src2 is not already in a register. |
| 130 | } else if (rl_src2.location != kLocPhysReg) { |
| 131 | OpRegImm64(kOpCmp, rl_src1.reg, val); |
| 132 | OpCondBranch(ccode, taken); |
| 133 | OpCondBranch(NegateComparison(ccode), not_taken); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | } |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 137 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 138 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 139 | OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 140 | OpCondBranch(ccode, taken); |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 141 | OpCondBranch(NegateComparison(ccode), not_taken); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Generate a register comparison to an immediate and branch. Caller |
| 146 | * is responsible for setting branch target field. |
| 147 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 148 | LIR* Arm64Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, |
| 149 | LIR* target) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 150 | LIR* branch; |
| 151 | ArmConditionCode arm_cond = ArmConditionEncoding(cond); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 152 | if (check_value == 0 && (arm_cond == kArmCondEq || arm_cond == kArmCondNe)) { |
| 153 | ArmOpcode opcode = (arm_cond == kArmCondEq) ? kA64Cbz2rt : kA64Cbnz2rt; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 154 | ArmOpcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0); |
| 155 | branch = NewLIR2(opcode | wide, reg.GetReg(), 0); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 156 | } else { |
| 157 | OpRegImm(kOpCmp, reg, check_value); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 158 | branch = NewLIR2(kA64B2ct, arm_cond, 0); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 159 | } |
| 160 | branch->target = target; |
| 161 | return branch; |
| 162 | } |
| 163 | |
Zheng Xu | 7c1c263 | 2014-06-17 18:17:31 +0800 | [diff] [blame] | 164 | LIR* Arm64Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, |
| 165 | RegStorage base_reg, int offset, int check_value, |
| 166 | LIR* target) { |
| 167 | // It is possible that temp register is 64-bit. (ArgReg or RefReg) |
| 168 | // Always compare 32-bit value no matter what temp_reg is. |
| 169 | if (temp_reg.Is64Bit()) { |
| 170 | temp_reg = As32BitReg(temp_reg); |
| 171 | } |
| 172 | Load32Disp(base_reg, offset, temp_reg); |
| 173 | LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target); |
| 174 | return branch; |
| 175 | } |
| 176 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 177 | LIR* Arm64Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 178 | bool dest_is_fp = r_dest.IsFloat(); |
| 179 | bool src_is_fp = r_src.IsFloat(); |
| 180 | ArmOpcode opcode = kA64Brk1d; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 181 | LIR* res; |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 182 | |
| 183 | if (LIKELY(dest_is_fp == src_is_fp)) { |
| 184 | if (LIKELY(!dest_is_fp)) { |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 185 | DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit()); |
| 186 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 187 | // Core/core copy. |
| 188 | // Copies involving the sp register require a different instruction. |
| 189 | opcode = UNLIKELY(A64_REG_IS_SP(r_dest.GetReg())) ? kA64Add4RRdT : kA64Mov2rr; |
| 190 | |
| 191 | // TODO(Arm64): kA64Add4RRdT formally has 4 args, but is used as a 2 args instruction. |
| 192 | // This currently works because the other arguments are set to 0 by default. We should |
| 193 | // rather introduce an alias kA64Mov2RR. |
| 194 | |
| 195 | // core/core copy. Do a x/x copy only if both registers are x. |
| 196 | if (r_dest.Is64Bit() && r_src.Is64Bit()) { |
| 197 | opcode = WIDE(opcode); |
| 198 | } |
| 199 | } else { |
| 200 | // Float/float copy. |
| 201 | bool dest_is_double = r_dest.IsDouble(); |
| 202 | bool src_is_double = r_src.IsDouble(); |
| 203 | |
| 204 | // We do not do float/double or double/float casts here. |
| 205 | DCHECK_EQ(dest_is_double, src_is_double); |
| 206 | |
| 207 | // Homogeneous float/float copy. |
| 208 | opcode = (dest_is_double) ? FWIDE(kA64Fmov2ff) : kA64Fmov2ff; |
| 209 | } |
| 210 | } else { |
| 211 | // Inhomogeneous register copy. |
| 212 | if (dest_is_fp) { |
| 213 | if (r_dest.IsDouble()) { |
| 214 | opcode = kA64Fmov2Sx; |
| 215 | } else { |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 216 | r_src = Check32BitReg(r_src); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 217 | opcode = kA64Fmov2sw; |
| 218 | } |
| 219 | } else { |
| 220 | if (r_src.IsDouble()) { |
| 221 | opcode = kA64Fmov2xS; |
| 222 | } else { |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 223 | r_dest = Check32BitReg(r_dest); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 224 | opcode = kA64Fmov2ws; |
| 225 | } |
| 226 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 227 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 228 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 229 | res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg()); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 230 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 231 | if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) { |
| 232 | res->flags.is_nop = true; |
| 233 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 234 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 235 | return res; |
| 236 | } |
| 237 | |
| 238 | void Arm64Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) { |
| 239 | if (r_dest != r_src) { |
| 240 | LIR* res = OpRegCopyNoInsert(r_dest, r_src); |
| 241 | AppendLIR(res); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void Arm64Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 246 | OpRegCopy(r_dest, r_src); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Table of magic divisors |
| 250 | struct MagicTable { |
| 251 | uint32_t magic; |
| 252 | uint32_t shift; |
| 253 | DividePattern pattern; |
| 254 | }; |
| 255 | |
| 256 | static const MagicTable magic_table[] = { |
| 257 | {0, 0, DivideNone}, // 0 |
| 258 | {0, 0, DivideNone}, // 1 |
| 259 | {0, 0, DivideNone}, // 2 |
| 260 | {0x55555556, 0, Divide3}, // 3 |
| 261 | {0, 0, DivideNone}, // 4 |
| 262 | {0x66666667, 1, Divide5}, // 5 |
| 263 | {0x2AAAAAAB, 0, Divide3}, // 6 |
| 264 | {0x92492493, 2, Divide7}, // 7 |
| 265 | {0, 0, DivideNone}, // 8 |
| 266 | {0x38E38E39, 1, Divide5}, // 9 |
| 267 | {0x66666667, 2, Divide5}, // 10 |
| 268 | {0x2E8BA2E9, 1, Divide5}, // 11 |
| 269 | {0x2AAAAAAB, 1, Divide5}, // 12 |
| 270 | {0x4EC4EC4F, 2, Divide5}, // 13 |
| 271 | {0x92492493, 3, Divide7}, // 14 |
| 272 | {0x88888889, 3, Divide7}, // 15 |
| 273 | }; |
| 274 | |
| 275 | // Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4) |
| 276 | bool Arm64Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 277 | RegLocation rl_src, RegLocation rl_dest, int lit) { |
| 278 | if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | DividePattern pattern = magic_table[lit].pattern; |
| 282 | if (pattern == DivideNone) { |
| 283 | return false; |
| 284 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 285 | // Tuning: add rem patterns |
| 286 | if (!is_div) { |
| 287 | return false; |
| 288 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 289 | |
| 290 | RegStorage r_magic = AllocTemp(); |
| 291 | LoadConstant(r_magic, magic_table[lit].magic); |
| 292 | rl_src = LoadValue(rl_src, kCoreReg); |
| 293 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 294 | RegStorage r_long_mul = AllocTemp(); |
| 295 | NewLIR4(kA64Smaddl4xwwx, As64BitReg(r_long_mul).GetReg(), |
| 296 | r_magic.GetReg(), rl_src.reg.GetReg(), rxzr); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 297 | switch (pattern) { |
| 298 | case Divide3: |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 299 | OpRegRegImm(kOpLsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul), 32); |
| 300 | OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 301 | break; |
| 302 | case Divide5: |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 303 | OpRegRegImm(kOpAsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul), |
| 304 | 32 + magic_table[lit].shift); |
| 305 | OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 306 | break; |
| 307 | case Divide7: |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 308 | OpRegRegRegShift(kOpAdd, As64BitReg(r_long_mul), As64BitReg(rl_src.reg), |
| 309 | As64BitReg(r_long_mul), EncodeShift(kA64Lsr, 32)); |
| 310 | OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift); |
| 311 | OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 312 | break; |
| 313 | default: |
| 314 | LOG(FATAL) << "Unexpected pattern: " << pattern; |
| 315 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 316 | StoreValue(rl_dest, rl_result); |
| 317 | return true; |
| 318 | } |
| 319 | |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 320 | // Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit' |
| 321 | // and store the result in 'rl_dest'. |
| 322 | bool Arm64Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div, |
| 323 | RegLocation rl_src, RegLocation rl_dest, int lit) { |
| 324 | if (lit < 2) { |
| 325 | return false; |
| 326 | } |
| 327 | if (!IsPowerOfTwo(lit)) { |
| 328 | return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit); |
| 329 | } |
| 330 | int k = LowestSetBit(lit); |
| 331 | if (k >= 30) { |
| 332 | // Avoid special cases. |
| 333 | return false; |
| 334 | } |
| 335 | rl_src = LoadValue(rl_src, kCoreReg); |
| 336 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 337 | if (is_div) { |
| 338 | RegStorage t_reg = AllocTemp(); |
| 339 | if (lit == 2) { |
| 340 | // Division by 2 is by far the most common division by constant. |
| 341 | OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k)); |
| 342 | OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k); |
| 343 | } else { |
| 344 | OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31); |
| 345 | OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, t_reg, EncodeShift(kA64Lsr, 32 - k)); |
| 346 | OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k); |
| 347 | } |
| 348 | } else { |
| 349 | RegStorage t_reg = AllocTemp(); |
| 350 | if (lit == 2) { |
| 351 | OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k)); |
| 352 | OpRegRegImm(kOpAnd, t_reg, t_reg, lit - 1); |
| 353 | OpRegRegRegShift(kOpSub, rl_result.reg, t_reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k)); |
| 354 | } else { |
| 355 | RegStorage t_reg2 = AllocTemp(); |
| 356 | OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31); |
| 357 | OpRegRegRegShift(kOpAdd, t_reg2, rl_src.reg, t_reg, EncodeShift(kA64Lsr, 32 - k)); |
| 358 | OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1); |
| 359 | OpRegRegRegShift(kOpSub, rl_result.reg, t_reg2, t_reg, EncodeShift(kA64Lsr, 32 - k)); |
| 360 | } |
| 361 | } |
| 362 | StoreValue(rl_dest, rl_result); |
| 363 | return true; |
| 364 | } |
| 365 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 366 | bool Arm64Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 367 | LOG(FATAL) << "Unexpected use of EasyMultiply for Arm64"; |
| 368 | return false; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1, |
Matteo Franchin | c61b3c9 | 2014-06-18 11:52:47 +0100 | [diff] [blame] | 372 | RegLocation rl_src2, bool is_div, bool check_zero) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 373 | LOG(FATAL) << "Unexpected use of GenDivRem for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 374 | return rl_dest; |
| 375 | } |
| 376 | |
| 377 | RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 378 | LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 379 | return rl_dest; |
| 380 | } |
| 381 | |
| 382 | RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) { |
| 383 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 384 | |
| 385 | // Put the literal in a temp. |
| 386 | RegStorage lit_temp = AllocTemp(); |
| 387 | LoadConstant(lit_temp, lit); |
| 388 | // Use the generic case for div/rem with arg2 in a register. |
| 389 | // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure. |
| 390 | rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div); |
| 391 | FreeTemp(lit_temp); |
| 392 | |
| 393 | return rl_result; |
| 394 | } |
| 395 | |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 396 | RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage r_src1, RegStorage r_src2, |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 397 | bool is_div) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 398 | CHECK_EQ(r_src1.Is64Bit(), r_src2.Is64Bit()); |
| 399 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 400 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 401 | if (is_div) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 402 | OpRegRegReg(kOpDiv, rl_result.reg, r_src1, r_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 403 | } else { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 404 | // temp = r_src1 / r_src2 |
| 405 | // dest = r_src1 - temp * r_src2 |
| 406 | RegStorage temp; |
| 407 | ArmOpcode wide; |
| 408 | if (rl_result.reg.Is64Bit()) { |
| 409 | temp = AllocTempWide(); |
| 410 | wide = WIDE(0); |
| 411 | } else { |
| 412 | temp = AllocTemp(); |
| 413 | wide = UNWIDE(0); |
| 414 | } |
| 415 | OpRegRegReg(kOpDiv, temp, r_src1, r_src2); |
| 416 | NewLIR4(kA64Msub4rrrr | wide, rl_result.reg.GetReg(), temp.GetReg(), |
| 417 | r_src1.GetReg(), r_src2.GetReg()); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 418 | FreeTemp(temp); |
| 419 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 420 | return rl_result; |
| 421 | } |
| 422 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 423 | bool Arm64Mir2Lir::GenInlinedAbsLong(CallInfo* info) { |
| 424 | RegLocation rl_src = info->args[0]; |
| 425 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 426 | RegLocation rl_dest = InlineTargetWide(info); |
| 427 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 428 | RegStorage sign_reg = AllocTempWide(); |
| 429 | // abs(x) = y<=x>>63, (x+y)^y. |
| 430 | OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63); |
| 431 | OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg); |
| 432 | OpRegReg(kOpXor, rl_result.reg, sign_reg); |
| 433 | StoreValueWide(rl_dest, rl_result); |
| 434 | return true; |
| 435 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 436 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 437 | bool Arm64Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) { |
| 438 | DCHECK_EQ(cu_->instruction_set, kArm64); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 439 | RegLocation rl_src1 = info->args[0]; |
| 440 | RegLocation rl_src2 = info->args[1]; |
| 441 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 442 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
| 443 | RegLocation rl_dest = InlineTarget(info); |
| 444 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 445 | OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 446 | NewLIR4(kA64Csel4rrrc, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), |
| 447 | rl_src2.reg.GetReg(), (is_min) ? kArmCondLt : kArmCondGt); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 448 | StoreValue(rl_dest, rl_result); |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | bool Arm64Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) { |
| 453 | RegLocation rl_src_address = info->args[0]; // long address |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 454 | rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1] ? |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 455 | RegLocation rl_dest = InlineTarget(info); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 456 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); // kRefReg |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 457 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 458 | |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 459 | LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 460 | if (size == k64) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 461 | StoreValueWide(rl_dest, rl_result); |
| 462 | } else { |
| 463 | DCHECK(size == kSignedByte || size == kSignedHalf || size == k32); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 464 | StoreValue(rl_dest, rl_result); |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | bool Arm64Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) { |
| 470 | RegLocation rl_src_address = info->args[0]; // long address |
| 471 | rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1] |
| 472 | RegLocation rl_src_value = info->args[2]; // [size] value |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 473 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); // kRefReg |
| 474 | |
| 475 | RegLocation rl_value; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 476 | if (size == k64) { |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 477 | rl_value = LoadValueWide(rl_src_value, kCoreReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 478 | } else { |
| 479 | DCHECK(size == kSignedByte || size == kSignedHalf || size == k32); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 480 | rl_value = LoadValue(rl_src_value, kCoreReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 481 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 482 | StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
| 486 | void Arm64Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 487 | LOG(FATAL) << "Unexpected use of OpLea for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 490 | void Arm64Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) { |
| 491 | UNIMPLEMENTED(FATAL) << "Should not be used."; |
| 492 | } |
| 493 | |
| 494 | void Arm64Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 495 | LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | bool Arm64Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) { |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 499 | DCHECK_EQ(cu_->instruction_set, kArm64); |
| 500 | ArmOpcode wide = is_long ? WIDE(0) : UNWIDE(0); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 501 | // Unused - RegLocation rl_src_unsafe = info->args[0]; |
| 502 | RegLocation rl_src_obj = info->args[1]; // Object - known non-null |
| 503 | RegLocation rl_src_offset = info->args[2]; // long low |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 504 | rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3] //TODO: do we really need this |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 505 | RegLocation rl_src_expected = info->args[4]; // int, long or Object |
| 506 | // If is_long, high half is in info->args[5] |
| 507 | RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object |
| 508 | // If is_long, high half is in info->args[7] |
| 509 | RegLocation rl_dest = InlineTarget(info); // boolean place for result |
| 510 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 511 | // Load Object and offset |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 512 | RegLocation rl_object = LoadValue(rl_src_obj, kRefReg); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 513 | RegLocation rl_offset = LoadValue(rl_src_offset, kRefReg); |
| 514 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 515 | RegLocation rl_new_value; |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 516 | RegLocation rl_expected; |
| 517 | if (is_long) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 518 | rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 519 | rl_expected = LoadValueWide(rl_src_expected, kCoreReg); |
| 520 | } else { |
| 521 | rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg); |
| 522 | rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) { |
| 526 | // Mark card for object assuming new value is stored. |
| 527 | MarkGCCard(rl_new_value.reg, rl_object.reg); |
| 528 | } |
| 529 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 530 | RegStorage r_ptr = AllocTempRef(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 531 | OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg); |
| 532 | |
| 533 | // Free now unneeded rl_object and rl_offset to give more temps. |
| 534 | ClobberSReg(rl_object.s_reg_low); |
| 535 | FreeTemp(rl_object.reg); |
| 536 | ClobberSReg(rl_offset.s_reg_low); |
| 537 | FreeTemp(rl_offset.reg); |
| 538 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 539 | // do { |
| 540 | // tmp = [r_ptr] - expected; |
| 541 | // } while (tmp == 0 && failure([r_ptr] <- r_new_value)); |
| 542 | // result = tmp != 0; |
| 543 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 544 | RegStorage r_tmp; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 545 | if (is_long) { |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 546 | r_tmp = AllocTempWide(); |
| 547 | } else if (is_object) { |
| 548 | r_tmp = AllocTempRef(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 549 | } else { |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 550 | r_tmp = AllocTemp(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 553 | LIR* loop = NewLIR0(kPseudoTargetLabel); |
| 554 | NewLIR2(kA64Ldaxr2rX | wide, r_tmp.GetReg(), r_ptr.GetReg()); |
| 555 | OpRegReg(kOpCmp, r_tmp, rl_expected.reg); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 556 | DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode)); |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 557 | LIR* early_exit = OpCondBranch(kCondNe, NULL); |
| 558 | |
| 559 | NewLIR3(kA64Stlxr3wrX | wide, As32BitReg(r_tmp).GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg()); |
| 560 | NewLIR3(kA64Cmp3RdT, As32BitReg(r_tmp).GetReg(), 0, ENCODE_NO_SHIFT); |
| 561 | DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode)); |
| 562 | OpCondBranch(kCondNe, loop); |
| 563 | |
| 564 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 565 | LIR* exit = NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondNe); |
| 566 | early_exit->target = exit; |
| 567 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 568 | FreeTemp(r_tmp); // Now unneeded. |
Serban Constantinescu | 169489b | 2014-06-11 16:43:35 +0100 | [diff] [blame] | 569 | FreeTemp(r_ptr); // Now unneeded. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 570 | |
| 571 | StoreValue(rl_dest, rl_result); |
| 572 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 573 | return true; |
| 574 | } |
| 575 | |
| 576 | LIR* Arm64Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 577 | return RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp), reg.GetReg(), 0, 0, 0, 0, target); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | LIR* Arm64Mir2Lir::OpVldm(RegStorage r_base, int count) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 581 | LOG(FATAL) << "Unexpected use of OpVldm for Arm64"; |
| 582 | return NULL; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | LIR* Arm64Mir2Lir::OpVstm(RegStorage r_base, int count) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 586 | LOG(FATAL) << "Unexpected use of OpVstm for Arm64"; |
| 587 | return NULL; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void Arm64Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src, |
| 591 | RegLocation rl_result, int lit, |
| 592 | int first_bit, int second_bit) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 593 | OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsl, second_bit - first_bit)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 594 | if (first_bit != 0) { |
| 595 | OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void Arm64Mir2Lir::GenDivZeroCheckWide(RegStorage reg) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 600 | LOG(FATAL) << "Unexpected use of GenDivZero for Arm64"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Test suspend flag, return target of taken suspend branch |
| 604 | LIR* Arm64Mir2Lir::OpTestSuspend(LIR* target) { |
Zheng Xu | baa7c88 | 2014-06-30 14:26:50 +0800 | [diff] [blame] | 605 | NewLIR3(kA64Subs3rRd, rwSUSPEND, rwSUSPEND, 1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 606 | return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target); |
| 607 | } |
| 608 | |
| 609 | // Decrement register and branch on condition |
| 610 | LIR* Arm64Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) { |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 611 | // Combine sub & test using sub setflags encoding here. We need to make sure a |
| 612 | // subtract form that sets carry is used, so generate explicitly. |
| 613 | // TODO: might be best to add a new op, kOpSubs, and handle it generically. |
| 614 | ArmOpcode opcode = reg.Is64Bit() ? WIDE(kA64Subs3rRd) : UNWIDE(kA64Subs3rRd); |
| 615 | NewLIR3(opcode, reg.GetReg(), reg.GetReg(), 1); // For value == 1, this should set flags. |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 616 | DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 617 | return OpCondBranch(c_code, target); |
| 618 | } |
| 619 | |
Andreas Gampe | b14329f | 2014-05-15 11:16:06 -0700 | [diff] [blame] | 620 | bool Arm64Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 621 | #if ANDROID_SMP != 0 |
| 622 | // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one. |
| 623 | LIR* barrier = last_lir_insn_; |
| 624 | |
| 625 | int dmb_flavor; |
| 626 | // TODO: revisit Arm barrier kinds |
| 627 | switch (barrier_kind) { |
| 628 | case kLoadStore: dmb_flavor = kISH; break; |
| 629 | case kLoadLoad: dmb_flavor = kISH; break; |
| 630 | case kStoreStore: dmb_flavor = kISHST; break; |
| 631 | case kStoreLoad: dmb_flavor = kISH; break; |
| 632 | default: |
| 633 | LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind; |
| 634 | dmb_flavor = kSY; // quiet gcc. |
| 635 | break; |
| 636 | } |
| 637 | |
Andreas Gampe | b14329f | 2014-05-15 11:16:06 -0700 | [diff] [blame] | 638 | bool ret = false; |
| 639 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 640 | // If the same barrier already exists, don't generate another. |
| 641 | if (barrier == nullptr |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 642 | || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) { |
| 643 | barrier = NewLIR1(kA64Dmb1B, dmb_flavor); |
Andreas Gampe | b14329f | 2014-05-15 11:16:06 -0700 | [diff] [blame] | 644 | ret = true; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // At this point we must have a memory barrier. Mark it as a scheduling barrier as well. |
| 648 | DCHECK(!barrier->flags.use_def_invalid); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 649 | barrier->u.m.def_mask = &kEncodeAll; |
Andreas Gampe | b14329f | 2014-05-15 11:16:06 -0700 | [diff] [blame] | 650 | return ret; |
| 651 | #else |
| 652 | return false; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 653 | #endif |
| 654 | } |
| 655 | |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 656 | void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) { |
| 657 | RegLocation rl_result; |
| 658 | |
| 659 | rl_src = LoadValue(rl_src, kCoreReg); |
| 660 | rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 661 | NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31); |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 662 | StoreValueWide(rl_dest, rl_result); |
| 663 | } |
| 664 | |
| 665 | void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest, |
| 666 | RegLocation rl_src1, RegLocation rl_src2, bool is_div) { |
| 667 | RegLocation rl_result; |
| 668 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 669 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 670 | GenDivZeroCheck(rl_src2.reg); |
| 671 | rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 672 | StoreValueWide(rl_dest, rl_result); |
| 673 | } |
| 674 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 675 | void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1, |
| 676 | RegLocation rl_src2) { |
| 677 | RegLocation rl_result; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 678 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 679 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 680 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 681 | rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 682 | OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT); |
| 683 | StoreValueWide(rl_dest, rl_result); |
| 684 | } |
| 685 | |
| 686 | void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) { |
| 687 | RegLocation rl_result; |
| 688 | |
| 689 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 690 | rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
| 691 | OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT); |
| 692 | StoreValueWide(rl_dest, rl_result); |
| 693 | } |
| 694 | |
| 695 | void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) { |
| 696 | RegLocation rl_result; |
| 697 | |
| 698 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 699 | rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
| 700 | OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 701 | StoreValueWide(rl_dest, rl_result); |
| 702 | } |
| 703 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 704 | void Arm64Mir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest, |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 705 | RegLocation rl_src1, RegLocation rl_src2) { |
| 706 | GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void Arm64Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 710 | RegLocation rl_src2) { |
| 711 | GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void Arm64Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
| 715 | RegLocation rl_src2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 716 | GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void Arm64Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
| 720 | RegLocation rl_src2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 721 | GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void Arm64Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
| 725 | RegLocation rl_src2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 726 | GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void Arm64Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1, |
| 730 | RegLocation rl_src2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 731 | GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Generate array load |
| 736 | */ |
| 737 | void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, |
| 738 | RegLocation rl_index, RegLocation rl_dest, int scale) { |
| 739 | RegisterClass reg_class = RegClassBySize(size); |
| 740 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
| 741 | int data_offset; |
| 742 | RegLocation rl_result; |
| 743 | bool constant_index = rl_index.is_const; |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 744 | rl_array = LoadValue(rl_array, kRefReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 745 | if (!constant_index) { |
| 746 | rl_index = LoadValue(rl_index, kCoreReg); |
| 747 | } |
| 748 | |
| 749 | if (rl_dest.wide) { |
| 750 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 751 | } else { |
| 752 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 753 | } |
| 754 | |
| 755 | // If index is constant, just fold it into the data offset |
| 756 | if (constant_index) { |
| 757 | data_offset += mir_graph_->ConstantValue(rl_index) << scale; |
| 758 | } |
| 759 | |
| 760 | /* null object? */ |
| 761 | GenNullCheck(rl_array.reg, opt_flags); |
| 762 | |
| 763 | bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK)); |
| 764 | RegStorage reg_len; |
| 765 | if (needs_range_check) { |
| 766 | reg_len = AllocTemp(); |
| 767 | /* Get len */ |
| 768 | Load32Disp(rl_array.reg, len_offset, reg_len); |
| 769 | MarkPossibleNullPointerException(opt_flags); |
| 770 | } else { |
| 771 | ForceImplicitNullCheck(rl_array.reg, opt_flags); |
| 772 | } |
| 773 | if (rl_dest.wide || rl_dest.fp || constant_index) { |
| 774 | RegStorage reg_ptr; |
| 775 | if (constant_index) { |
| 776 | reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case. |
| 777 | } else { |
| 778 | // No special indexed operation, lea + load w/ displacement |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 779 | reg_ptr = AllocTempRef(); |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 780 | OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg), |
| 781 | EncodeShift(kA64Lsl, scale)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 782 | FreeTemp(rl_index.reg); |
| 783 | } |
| 784 | rl_result = EvalLoc(rl_dest, reg_class, true); |
| 785 | |
| 786 | if (needs_range_check) { |
| 787 | if (constant_index) { |
| 788 | GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len); |
| 789 | } else { |
| 790 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
| 791 | } |
| 792 | FreeTemp(reg_len); |
| 793 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 794 | if (rl_result.ref) { |
| 795 | LoadRefDisp(reg_ptr, data_offset, rl_result.reg, kNotVolatile); |
| 796 | } else { |
| 797 | LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile); |
| 798 | } |
Vladimir Marko | 455759b | 2014-05-06 20:49:36 +0100 | [diff] [blame] | 799 | MarkPossibleNullPointerException(opt_flags); |
| 800 | if (!constant_index) { |
| 801 | FreeTemp(reg_ptr); |
| 802 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 803 | if (rl_dest.wide) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 804 | StoreValueWide(rl_dest, rl_result); |
| 805 | } else { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 806 | StoreValue(rl_dest, rl_result); |
| 807 | } |
| 808 | } else { |
| 809 | // Offset base, then use indexed load |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 810 | RegStorage reg_ptr = AllocTempRef(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 811 | OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset); |
| 812 | FreeTemp(rl_array.reg); |
| 813 | rl_result = EvalLoc(rl_dest, reg_class, true); |
| 814 | |
| 815 | if (needs_range_check) { |
| 816 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
| 817 | FreeTemp(reg_len); |
| 818 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 819 | if (rl_result.ref) { |
| 820 | LoadRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg); |
| 821 | } else { |
| 822 | LoadBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale, size); |
| 823 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 824 | MarkPossibleNullPointerException(opt_flags); |
| 825 | FreeTemp(reg_ptr); |
| 826 | StoreValue(rl_dest, rl_result); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Generate array store |
| 832 | * |
| 833 | */ |
| 834 | void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array, |
| 835 | RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) { |
| 836 | RegisterClass reg_class = RegClassBySize(size); |
| 837 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
| 838 | bool constant_index = rl_index.is_const; |
| 839 | |
| 840 | int data_offset; |
| 841 | if (size == k64 || size == kDouble) { |
| 842 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 843 | } else { |
| 844 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 845 | } |
| 846 | |
| 847 | // If index is constant, just fold it into the data offset. |
| 848 | if (constant_index) { |
| 849 | data_offset += mir_graph_->ConstantValue(rl_index) << scale; |
| 850 | } |
| 851 | |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 852 | rl_array = LoadValue(rl_array, kRefReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 853 | if (!constant_index) { |
| 854 | rl_index = LoadValue(rl_index, kCoreReg); |
| 855 | } |
| 856 | |
| 857 | RegStorage reg_ptr; |
| 858 | bool allocated_reg_ptr_temp = false; |
| 859 | if (constant_index) { |
| 860 | reg_ptr = rl_array.reg; |
| 861 | } else if (IsTemp(rl_array.reg) && !card_mark) { |
| 862 | Clobber(rl_array.reg); |
| 863 | reg_ptr = rl_array.reg; |
| 864 | } else { |
| 865 | allocated_reg_ptr_temp = true; |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 866 | reg_ptr = AllocTempRef(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* null object? */ |
| 870 | GenNullCheck(rl_array.reg, opt_flags); |
| 871 | |
| 872 | bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK)); |
| 873 | RegStorage reg_len; |
| 874 | if (needs_range_check) { |
| 875 | reg_len = AllocTemp(); |
| 876 | // NOTE: max live temps(4) here. |
| 877 | /* Get len */ |
| 878 | Load32Disp(rl_array.reg, len_offset, reg_len); |
| 879 | MarkPossibleNullPointerException(opt_flags); |
| 880 | } else { |
| 881 | ForceImplicitNullCheck(rl_array.reg, opt_flags); |
| 882 | } |
| 883 | /* at this point, reg_ptr points to array, 2 live temps */ |
| 884 | if (rl_src.wide || rl_src.fp || constant_index) { |
| 885 | if (rl_src.wide) { |
| 886 | rl_src = LoadValueWide(rl_src, reg_class); |
| 887 | } else { |
| 888 | rl_src = LoadValue(rl_src, reg_class); |
| 889 | } |
| 890 | if (!constant_index) { |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 891 | OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg), |
| 892 | EncodeShift(kA64Lsl, scale)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 893 | } |
| 894 | if (needs_range_check) { |
| 895 | if (constant_index) { |
| 896 | GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len); |
| 897 | } else { |
| 898 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
| 899 | } |
| 900 | FreeTemp(reg_len); |
| 901 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 902 | if (rl_src.ref) { |
| 903 | StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile); |
| 904 | } else { |
| 905 | StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile); |
| 906 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 907 | MarkPossibleNullPointerException(opt_flags); |
| 908 | } else { |
| 909 | /* reg_ptr -> array data */ |
| 910 | OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset); |
| 911 | rl_src = LoadValue(rl_src, reg_class); |
| 912 | if (needs_range_check) { |
| 913 | GenArrayBoundsCheck(rl_index.reg, reg_len); |
| 914 | FreeTemp(reg_len); |
| 915 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 916 | if (rl_src.ref) { |
| 917 | StoreRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg); |
| 918 | } else { |
| 919 | StoreBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale, size); |
| 920 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 921 | MarkPossibleNullPointerException(opt_flags); |
| 922 | } |
| 923 | if (allocated_reg_ptr_temp) { |
| 924 | FreeTemp(reg_ptr); |
| 925 | } |
| 926 | if (card_mark) { |
| 927 | MarkGCCard(rl_src.reg, rl_array.reg); |
| 928 | } |
| 929 | } |
| 930 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 931 | void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, |
| 932 | RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 933 | OpKind op = kOpBkpt; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 934 | // Per spec, we only care about low 6 bits of shift amount. |
| 935 | int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 936 | rl_src = LoadValueWide(rl_src, kCoreReg); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 937 | if (shift_amount == 0) { |
| 938 | StoreValueWide(rl_dest, rl_src); |
| 939 | return; |
| 940 | } |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 941 | |
| 942 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 943 | switch (opcode) { |
| 944 | case Instruction::SHL_LONG: |
| 945 | case Instruction::SHL_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 946 | op = kOpLsl; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 947 | break; |
| 948 | case Instruction::SHR_LONG: |
| 949 | case Instruction::SHR_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 950 | op = kOpAsr; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 951 | break; |
| 952 | case Instruction::USHR_LONG: |
| 953 | case Instruction::USHR_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 954 | op = kOpLsr; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 955 | break; |
| 956 | default: |
| 957 | LOG(FATAL) << "Unexpected case"; |
| 958 | } |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 959 | OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 960 | StoreValueWide(rl_dest, rl_result); |
| 961 | } |
| 962 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 963 | void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest, |
| 964 | RegLocation rl_src1, RegLocation rl_src2) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 965 | if ((opcode == Instruction::SUB_LONG) || (opcode == Instruction::SUB_LONG_2ADDR)) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 966 | if (!rl_src2.is_const) { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 967 | return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 968 | } |
| 969 | } else { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 970 | // Associativity. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 971 | if (!rl_src2.is_const) { |
| 972 | DCHECK(rl_src1.is_const); |
| 973 | std::swap(rl_src1, rl_src2); |
| 974 | } |
| 975 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 976 | DCHECK(rl_src2.is_const); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 977 | |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 978 | OpKind op = kOpBkpt; |
| 979 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 980 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 981 | switch (opcode) { |
| 982 | case Instruction::ADD_LONG: |
| 983 | case Instruction::ADD_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 984 | op = kOpAdd; |
| 985 | break; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 986 | case Instruction::SUB_LONG: |
| 987 | case Instruction::SUB_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 988 | op = kOpSub; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 989 | break; |
| 990 | case Instruction::AND_LONG: |
| 991 | case Instruction::AND_LONG_2ADDR: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 992 | op = kOpAnd; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 993 | break; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 994 | case Instruction::OR_LONG: |
| 995 | case Instruction::OR_LONG_2ADDR: |
| 996 | op = kOpOr; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 997 | break; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 998 | case Instruction::XOR_LONG: |
| 999 | case Instruction::XOR_LONG_2ADDR: |
| 1000 | op = kOpXor; |
| 1001 | break; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 1002 | default: |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 1003 | LOG(FATAL) << "Unexpected opcode"; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 1004 | } |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 1005 | |
| 1006 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1007 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
Zheng Xu | e2eb29e | 2014-06-12 10:22:33 +0800 | [diff] [blame] | 1008 | OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 1009 | StoreValueWide(rl_dest, rl_result); |
| 1010 | } |
| 1011 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1012 | /** |
| 1013 | * @brief Split a register list in pairs or registers. |
| 1014 | * |
| 1015 | * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows: |
| 1016 | * @code |
| 1017 | * int reg1 = -1, reg2 = -1; |
| 1018 | * while (reg_mask) { |
| 1019 | * reg_mask = GenPairWise(reg_mask, & reg1, & reg2); |
| 1020 | * if (UNLIKELY(reg2 < 0)) { |
| 1021 | * // Single register in reg1. |
| 1022 | * } else { |
| 1023 | * // Pair in reg1, reg2. |
| 1024 | * } |
| 1025 | * } |
| 1026 | * @endcode |
| 1027 | */ |
| 1028 | uint32_t Arm64Mir2Lir::GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) { |
| 1029 | // Find first register. |
| 1030 | int first_bit_set = __builtin_ctz(reg_mask) + 1; |
| 1031 | int reg = *reg1 + first_bit_set; |
| 1032 | reg_mask >>= first_bit_set; |
| 1033 | |
| 1034 | if (LIKELY(reg_mask)) { |
| 1035 | // Save the first register, find the second and use the pair opcode. |
| 1036 | int second_bit_set = __builtin_ctz(reg_mask) + 1; |
| 1037 | *reg2 = reg; |
| 1038 | reg_mask >>= second_bit_set; |
| 1039 | *reg1 = reg + second_bit_set; |
| 1040 | return reg_mask; |
| 1041 | } |
| 1042 | |
| 1043 | // Use the single opcode, as we just have one register. |
| 1044 | *reg1 = reg; |
| 1045 | *reg2 = -1; |
| 1046 | return reg_mask; |
| 1047 | } |
| 1048 | |
| 1049 | void Arm64Mir2Lir::UnSpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) { |
| 1050 | int reg1 = -1, reg2 = -1; |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1051 | const int reg_log2_size = 3; |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1052 | |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1053 | for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1054 | reg_mask = GenPairWise(reg_mask, & reg1, & reg2); |
| 1055 | if (UNLIKELY(reg2 < 0)) { |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1056 | NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1057 | } else { |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame^] | 1058 | DCHECK_LE(offset, 63); |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1059 | NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(), |
| 1060 | RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | void Arm64Mir2Lir::SpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) { |
| 1066 | int reg1 = -1, reg2 = -1; |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1067 | const int reg_log2_size = 3; |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1068 | |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1069 | for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1070 | reg_mask = GenPairWise(reg_mask, & reg1, & reg2); |
| 1071 | if (UNLIKELY(reg2 < 0)) { |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1072 | NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1073 | } else { |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 1074 | NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(), |
| 1075 | RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | void Arm64Mir2Lir::UnSpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) { |
| 1081 | int reg1 = -1, reg2 = -1; |
| 1082 | const int reg_log2_size = 3; |
| 1083 | |
| 1084 | for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) { |
| 1085 | reg_mask = GenPairWise(reg_mask, & reg1, & reg2); |
| 1086 | if (UNLIKELY(reg2 < 0)) { |
| 1087 | NewLIR3(FWIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset); |
| 1088 | } else { |
| 1089 | NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(), |
| 1090 | RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | // TODO(Arm64): consider using ld1 and st1? |
| 1096 | void Arm64Mir2Lir::SpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) { |
| 1097 | int reg1 = -1, reg2 = -1; |
| 1098 | const int reg_log2_size = 3; |
| 1099 | |
| 1100 | for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) { |
| 1101 | reg_mask = GenPairWise(reg_mask, & reg1, & reg2); |
| 1102 | if (UNLIKELY(reg2 < 0)) { |
| 1103 | NewLIR3(FWIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset); |
| 1104 | } else { |
| 1105 | NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(), |
| 1106 | RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 1111 | } // namespace art |