Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 X86 ISA */ |
| 18 | |
| 19 | #include "codegen_x86.h" |
| 20 | #include "dex/quick/mir_to_lir-inl.h" |
| 21 | #include "mirror/array.h" |
| 22 | #include "x86_lir.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | /* |
| 27 | * Perform register memory operation. |
| 28 | */ |
| 29 | LIR* X86Mir2Lir::GenRegMemCheck(ConditionCode c_code, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 30 | int reg1, int base, int offset, ThrowKind kind) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, |
| 32 | current_dalvik_offset_, reg1, base, offset); |
| 33 | OpRegMem(kOpCmp, reg1, base, offset); |
| 34 | LIR* branch = OpCondBranch(c_code, tgt); |
| 35 | // Remember branch target - will process later |
| 36 | throw_launchpads_.Insert(tgt); |
| 37 | return branch; |
| 38 | } |
| 39 | |
| 40 | /* |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 41 | * Perform a compare of memory to immediate value |
| 42 | */ |
| 43 | LIR* X86Mir2Lir::GenMemImmedCheck(ConditionCode c_code, |
| 44 | int base, int offset, int check_value, ThrowKind kind) { |
| 45 | LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, |
| 46 | current_dalvik_offset_, base, check_value, 0); |
| 47 | NewLIR3(IS_SIMM8(check_value) ? kX86Cmp32MI8 : kX86Cmp32MI, base, offset, check_value); |
| 48 | LIR* branch = OpCondBranch(c_code, tgt); |
| 49 | // Remember branch target - will process later |
| 50 | throw_launchpads_.Insert(tgt); |
| 51 | return branch; |
| 52 | } |
| 53 | |
| 54 | /* |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 55 | * Compare two 64-bit values |
| 56 | * x = y return 0 |
| 57 | * x < y return -1 |
| 58 | * x > y return 1 |
| 59 | */ |
| 60 | void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 61 | RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 62 | FlushAllRegs(); |
| 63 | LockCallTemps(); // Prepare for explicit register usage |
| 64 | LoadValueDirectWideFixed(rl_src1, r0, r1); |
| 65 | LoadValueDirectWideFixed(rl_src2, r2, r3); |
| 66 | // Compute (r1:r0) = (r1:r0) - (r3:r2) |
| 67 | OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2 |
| 68 | OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF |
| 69 | NewLIR2(kX86Set8R, r2, kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0 |
| 70 | NewLIR2(kX86Movzx8RR, r2, r2); |
| 71 | OpReg(kOpNeg, r2); // r2 = -r2 |
| 72 | OpRegReg(kOpOr, r0, r1); // r0 = high | low - sets ZF |
| 73 | NewLIR2(kX86Set8R, r0, kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0 |
| 74 | NewLIR2(kX86Movzx8RR, r0, r0); |
| 75 | OpRegReg(kOpOr, r0, r2); // r0 = r0 | r2 |
| 76 | RegLocation rl_result = LocCReturn(); |
| 77 | StoreValue(rl_dest, rl_result); |
| 78 | } |
| 79 | |
| 80 | X86ConditionCode X86ConditionEncoding(ConditionCode cond) { |
| 81 | switch (cond) { |
| 82 | case kCondEq: return kX86CondEq; |
| 83 | case kCondNe: return kX86CondNe; |
| 84 | case kCondCs: return kX86CondC; |
| 85 | case kCondCc: return kX86CondNc; |
Vladimir Marko | 58af1f9 | 2013-12-19 13:31:15 +0000 | [diff] [blame] | 86 | case kCondUlt: return kX86CondC; |
| 87 | case kCondUge: return kX86CondNc; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 88 | case kCondMi: return kX86CondS; |
| 89 | case kCondPl: return kX86CondNs; |
| 90 | case kCondVs: return kX86CondO; |
| 91 | case kCondVc: return kX86CondNo; |
| 92 | case kCondHi: return kX86CondA; |
| 93 | case kCondLs: return kX86CondBe; |
| 94 | case kCondGe: return kX86CondGe; |
| 95 | case kCondLt: return kX86CondL; |
| 96 | case kCondGt: return kX86CondG; |
| 97 | case kCondLe: return kX86CondLe; |
| 98 | case kCondAl: |
| 99 | case kCondNv: LOG(FATAL) << "Should not reach here"; |
| 100 | } |
| 101 | return kX86CondO; |
| 102 | } |
| 103 | |
| 104 | LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, int src1, int src2, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 105 | LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | NewLIR2(kX86Cmp32RR, src1, src2); |
| 107 | X86ConditionCode cc = X86ConditionEncoding(cond); |
| 108 | LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , |
| 109 | cc); |
| 110 | branch->target = target; |
| 111 | return branch; |
| 112 | } |
| 113 | |
| 114 | LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, int reg, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 115 | int check_value, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 116 | if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) { |
| 117 | // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode |
| 118 | NewLIR2(kX86Test32RR, reg, reg); |
| 119 | } else { |
| 120 | NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg, check_value); |
| 121 | } |
| 122 | X86ConditionCode cc = X86ConditionEncoding(cond); |
| 123 | LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc); |
| 124 | branch->target = target; |
| 125 | return branch; |
| 126 | } |
| 127 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 128 | LIR* X86Mir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 129 | if (X86_FPREG(r_dest) || X86_FPREG(r_src)) |
| 130 | return OpFpRegCopy(r_dest, r_src); |
| 131 | LIR* res = RawLIR(current_dalvik_offset_, kX86Mov32RR, |
| 132 | r_dest, r_src); |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 133 | if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 134 | res->flags.is_nop = true; |
| 135 | } |
| 136 | return res; |
| 137 | } |
| 138 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 139 | LIR* X86Mir2Lir::OpRegCopy(int r_dest, int r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 140 | LIR *res = OpRegCopyNoInsert(r_dest, r_src); |
| 141 | AppendLIR(res); |
| 142 | return res; |
| 143 | } |
| 144 | |
| 145 | void X86Mir2Lir::OpRegCopyWide(int dest_lo, int dest_hi, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 146 | int src_lo, int src_hi) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 147 | bool dest_fp = X86_FPREG(dest_lo) && X86_FPREG(dest_hi); |
| 148 | bool src_fp = X86_FPREG(src_lo) && X86_FPREG(src_hi); |
| 149 | assert(X86_FPREG(src_lo) == X86_FPREG(src_hi)); |
| 150 | assert(X86_FPREG(dest_lo) == X86_FPREG(dest_hi)); |
| 151 | if (dest_fp) { |
| 152 | if (src_fp) { |
| 153 | OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi)); |
| 154 | } else { |
| 155 | // TODO: Prevent this from happening in the code. The result is often |
| 156 | // unused or could have been loaded more easily from memory. |
| 157 | NewLIR2(kX86MovdxrRR, dest_lo, src_lo); |
Bill Buzbee | d61ba4b | 2014-01-13 21:44:01 +0000 | [diff] [blame] | 158 | dest_hi = AllocTempDouble(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | NewLIR2(kX86MovdxrRR, dest_hi, src_hi); |
Razvan A Lupusoru | f43adf6 | 2014-01-28 09:25:52 -0800 | [diff] [blame] | 160 | NewLIR2(kX86PunpckldqRR, dest_lo, dest_hi); |
Bill Buzbee | d61ba4b | 2014-01-13 21:44:01 +0000 | [diff] [blame] | 161 | FreeTemp(dest_hi); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 162 | } |
| 163 | } else { |
| 164 | if (src_fp) { |
| 165 | NewLIR2(kX86MovdrxRR, dest_lo, src_lo); |
| 166 | NewLIR2(kX86PsrlqRI, src_lo, 32); |
| 167 | NewLIR2(kX86MovdrxRR, dest_hi, src_lo); |
| 168 | } else { |
| 169 | // Handle overlap |
| 170 | if (src_hi == dest_lo) { |
| 171 | OpRegCopy(dest_hi, src_hi); |
| 172 | OpRegCopy(dest_lo, src_lo); |
| 173 | } else { |
| 174 | OpRegCopy(dest_lo, src_lo); |
| 175 | OpRegCopy(dest_hi, src_hi); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 181 | void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) { |
Razvan A Lupusoru | e27b3bf | 2014-01-23 09:41:45 -0800 | [diff] [blame] | 182 | RegLocation rl_result; |
| 183 | RegLocation rl_src = mir_graph_->GetSrc(mir, 0); |
| 184 | RegLocation rl_dest = mir_graph_->GetDest(mir); |
| 185 | rl_src = LoadValue(rl_src, kCoreReg); |
| 186 | |
| 187 | // The kMirOpSelect has two variants, one for constants and one for moves. |
| 188 | const bool is_constant_case = (mir->ssa_rep->num_uses == 1); |
| 189 | |
| 190 | if (is_constant_case) { |
| 191 | int true_val = mir->dalvikInsn.vB; |
| 192 | int false_val = mir->dalvikInsn.vC; |
| 193 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 194 | |
| 195 | /* |
| 196 | * 1) When the true case is zero and result_reg is not same as src_reg: |
| 197 | * xor result_reg, result_reg |
| 198 | * cmp $0, src_reg |
| 199 | * mov t1, $false_case |
| 200 | * cmovnz result_reg, t1 |
| 201 | * 2) When the false case is zero and result_reg is not same as src_reg: |
| 202 | * xor result_reg, result_reg |
| 203 | * cmp $0, src_reg |
| 204 | * mov t1, $true_case |
| 205 | * cmovz result_reg, t1 |
| 206 | * 3) All other cases (we do compare first to set eflags): |
| 207 | * cmp $0, src_reg |
| 208 | * mov result_reg, $true_case |
| 209 | * mov t1, $false_case |
| 210 | * cmovnz result_reg, t1 |
| 211 | */ |
| 212 | const bool result_reg_same_as_src = (rl_src.location == kLocPhysReg && rl_src.low_reg == rl_result.low_reg); |
| 213 | const bool true_zero_case = (true_val == 0 && false_val != 0 && !result_reg_same_as_src); |
| 214 | const bool false_zero_case = (false_val == 0 && true_val != 0 && !result_reg_same_as_src); |
| 215 | const bool catch_all_case = !(true_zero_case || false_zero_case); |
| 216 | |
| 217 | if (true_zero_case || false_zero_case) { |
| 218 | OpRegReg(kOpXor, rl_result.low_reg, rl_result.low_reg); |
| 219 | } |
| 220 | |
| 221 | if (true_zero_case || false_zero_case || catch_all_case) { |
| 222 | OpRegImm(kOpCmp, rl_src.low_reg, 0); |
| 223 | } |
| 224 | |
| 225 | if (catch_all_case) { |
| 226 | OpRegImm(kOpMov, rl_result.low_reg, true_val); |
| 227 | } |
| 228 | |
| 229 | if (true_zero_case || false_zero_case || catch_all_case) { |
| 230 | int immediateForTemp = false_zero_case ? true_val : false_val; |
| 231 | int temp1_reg = AllocTemp(); |
| 232 | OpRegImm(kOpMov, temp1_reg, immediateForTemp); |
| 233 | |
| 234 | ConditionCode cc = false_zero_case ? kCondEq : kCondNe; |
| 235 | OpCondRegReg(kOpCmov, cc, rl_result.low_reg, temp1_reg); |
| 236 | |
| 237 | FreeTemp(temp1_reg); |
| 238 | } |
| 239 | } else { |
| 240 | RegLocation rl_true = mir_graph_->GetSrc(mir, 1); |
| 241 | RegLocation rl_false = mir_graph_->GetSrc(mir, 2); |
| 242 | rl_true = LoadValue(rl_true, kCoreReg); |
| 243 | rl_false = LoadValue(rl_false, kCoreReg); |
| 244 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 245 | |
| 246 | /* |
| 247 | * 1) When true case is already in place: |
| 248 | * cmp $0, src_reg |
| 249 | * cmovnz result_reg, false_reg |
| 250 | * 2) When false case is already in place: |
| 251 | * cmp $0, src_reg |
| 252 | * cmovz result_reg, true_reg |
| 253 | * 3) When neither cases are in place: |
| 254 | * cmp $0, src_reg |
| 255 | * mov result_reg, true_reg |
| 256 | * cmovnz result_reg, false_reg |
| 257 | */ |
| 258 | |
| 259 | // kMirOpSelect is generated just for conditional cases when comparison is done with zero. |
| 260 | OpRegImm(kOpCmp, rl_src.low_reg, 0); |
| 261 | |
| 262 | if (rl_result.low_reg == rl_true.low_reg) { |
| 263 | OpCondRegReg(kOpCmov, kCondNe, rl_result.low_reg, rl_false.low_reg); |
| 264 | } else if (rl_result.low_reg == rl_false.low_reg) { |
| 265 | OpCondRegReg(kOpCmov, kCondEq, rl_result.low_reg, rl_true.low_reg); |
| 266 | } else { |
| 267 | OpRegCopy(rl_result.low_reg, rl_true.low_reg); |
| 268 | OpCondRegReg(kOpCmov, kCondNe, rl_result.low_reg, rl_false.low_reg); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | StoreValue(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 276 | LIR* taken = &block_label_list_[bb->taken]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 277 | RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0); |
| 278 | RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2); |
Vladimir Marko | a894607 | 2014-01-22 10:30:44 +0000 | [diff] [blame] | 279 | ConditionCode ccode = mir->meta.ccode; |
Mark Mendell | 412d4f8 | 2013-12-18 13:32:36 -0800 | [diff] [blame] | 280 | |
| 281 | if (rl_src1.is_const) { |
| 282 | std::swap(rl_src1, rl_src2); |
| 283 | ccode = FlipComparisonOrder(ccode); |
| 284 | } |
| 285 | if (rl_src2.is_const) { |
| 286 | // Do special compare/branch against simple const operand |
| 287 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 288 | GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode); |
| 289 | return; |
| 290 | } |
| 291 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 292 | FlushAllRegs(); |
| 293 | LockCallTemps(); // Prepare for explicit register usage |
| 294 | LoadValueDirectWideFixed(rl_src1, r0, r1); |
| 295 | LoadValueDirectWideFixed(rl_src2, r2, r3); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 296 | // Swap operands and condition code to prevent use of zero flag. |
| 297 | if (ccode == kCondLe || ccode == kCondGt) { |
| 298 | // Compute (r3:r2) = (r3:r2) - (r1:r0) |
| 299 | OpRegReg(kOpSub, r2, r0); // r2 = r2 - r0 |
| 300 | OpRegReg(kOpSbc, r3, r1); // r3 = r3 - r1 - CF |
| 301 | } else { |
| 302 | // Compute (r1:r0) = (r1:r0) - (r3:r2) |
| 303 | OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2 |
| 304 | OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF |
| 305 | } |
| 306 | switch (ccode) { |
| 307 | case kCondEq: |
| 308 | case kCondNe: |
| 309 | OpRegReg(kOpOr, r0, r1); // r0 = r0 | r1 |
| 310 | break; |
| 311 | case kCondLe: |
| 312 | ccode = kCondGe; |
| 313 | break; |
| 314 | case kCondGt: |
| 315 | ccode = kCondLt; |
| 316 | break; |
| 317 | case kCondLt: |
| 318 | case kCondGe: |
| 319 | break; |
| 320 | default: |
| 321 | LOG(FATAL) << "Unexpected ccode: " << ccode; |
| 322 | } |
| 323 | OpCondBranch(ccode, taken); |
| 324 | } |
| 325 | |
Mark Mendell | 412d4f8 | 2013-12-18 13:32:36 -0800 | [diff] [blame] | 326 | void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1, |
| 327 | int64_t val, ConditionCode ccode) { |
| 328 | int32_t val_lo = Low32Bits(val); |
| 329 | int32_t val_hi = High32Bits(val); |
| 330 | LIR* taken = &block_label_list_[bb->taken]; |
| 331 | LIR* not_taken = &block_label_list_[bb->fall_through]; |
| 332 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 333 | int32_t low_reg = rl_src1.low_reg; |
| 334 | int32_t high_reg = rl_src1.high_reg; |
| 335 | |
| 336 | if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) { |
| 337 | int t_reg = AllocTemp(); |
| 338 | OpRegRegReg(kOpOr, t_reg, low_reg, high_reg); |
| 339 | FreeTemp(t_reg); |
| 340 | OpCondBranch(ccode, taken); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | OpRegImm(kOpCmp, high_reg, val_hi); |
| 345 | switch (ccode) { |
| 346 | case kCondEq: |
| 347 | case kCondNe: |
| 348 | OpCondBranch(kCondNe, (ccode == kCondEq) ? not_taken : taken); |
| 349 | break; |
| 350 | case kCondLt: |
| 351 | OpCondBranch(kCondLt, taken); |
| 352 | OpCondBranch(kCondGt, not_taken); |
| 353 | ccode = kCondUlt; |
| 354 | break; |
| 355 | case kCondLe: |
| 356 | OpCondBranch(kCondLt, taken); |
| 357 | OpCondBranch(kCondGt, not_taken); |
| 358 | ccode = kCondLs; |
| 359 | break; |
| 360 | case kCondGt: |
| 361 | OpCondBranch(kCondGt, taken); |
| 362 | OpCondBranch(kCondLt, not_taken); |
| 363 | ccode = kCondHi; |
| 364 | break; |
| 365 | case kCondGe: |
| 366 | OpCondBranch(kCondGt, taken); |
| 367 | OpCondBranch(kCondLt, not_taken); |
| 368 | ccode = kCondUge; |
| 369 | break; |
| 370 | default: |
| 371 | LOG(FATAL) << "Unexpected ccode: " << ccode; |
| 372 | } |
| 373 | OpCmpImmBranch(ccode, low_reg, val_lo, taken); |
| 374 | } |
| 375 | |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 376 | void X86Mir2Lir::CalculateMagicAndShift(int divisor, int& magic, int& shift) { |
| 377 | // It does not make sense to calculate magic and shift for zero divisor. |
| 378 | DCHECK_NE(divisor, 0); |
| 379 | |
| 380 | /* According to H.S.Warren's Hacker's Delight Chapter 10 and |
| 381 | * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication. |
| 382 | * The magic number M and shift S can be calculated in the following way: |
| 383 | * Let nc be the most positive value of numerator(n) such that nc = kd - 1, |
| 384 | * where divisor(d) >=2. |
| 385 | * Let nc be the most negative value of numerator(n) such that nc = kd + 1, |
| 386 | * where divisor(d) <= -2. |
| 387 | * Thus nc can be calculated like: |
| 388 | * nc = 2^31 + 2^31 % d - 1, where d >= 2 |
| 389 | * nc = -2^31 + (2^31 + 1) % d, where d >= 2. |
| 390 | * |
| 391 | * So the shift p is the smallest p satisfying |
| 392 | * 2^p > nc * (d - 2^p % d), where d >= 2 |
| 393 | * 2^p > nc * (d + 2^p % d), where d <= -2. |
| 394 | * |
| 395 | * the magic number M is calcuated by |
| 396 | * M = (2^p + d - 2^p % d) / d, where d >= 2 |
| 397 | * M = (2^p - d - 2^p % d) / d, where d <= -2. |
| 398 | * |
| 399 | * Notice that p is always bigger than or equal to 32, so we just return 32-p as |
| 400 | * the shift number S. |
| 401 | */ |
| 402 | |
| 403 | int32_t p = 31; |
| 404 | const uint32_t two31 = 0x80000000U; |
| 405 | |
| 406 | // Initialize the computations. |
| 407 | uint32_t abs_d = (divisor >= 0) ? divisor : -divisor; |
| 408 | uint32_t tmp = two31 + (static_cast<uint32_t>(divisor) >> 31); |
| 409 | uint32_t abs_nc = tmp - 1 - tmp % abs_d; |
| 410 | uint32_t quotient1 = two31 / abs_nc; |
| 411 | uint32_t remainder1 = two31 % abs_nc; |
| 412 | uint32_t quotient2 = two31 / abs_d; |
| 413 | uint32_t remainder2 = two31 % abs_d; |
| 414 | |
| 415 | /* |
| 416 | * To avoid handling both positive and negative divisor, Hacker's Delight |
| 417 | * introduces a method to handle these 2 cases together to avoid duplication. |
| 418 | */ |
| 419 | uint32_t delta; |
| 420 | do { |
| 421 | p++; |
| 422 | quotient1 = 2 * quotient1; |
| 423 | remainder1 = 2 * remainder1; |
| 424 | if (remainder1 >= abs_nc) { |
| 425 | quotient1++; |
| 426 | remainder1 = remainder1 - abs_nc; |
| 427 | } |
| 428 | quotient2 = 2 * quotient2; |
| 429 | remainder2 = 2 * remainder2; |
| 430 | if (remainder2 >= abs_d) { |
| 431 | quotient2++; |
| 432 | remainder2 = remainder2 - abs_d; |
| 433 | } |
| 434 | delta = abs_d - remainder2; |
| 435 | } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0)); |
| 436 | |
| 437 | magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1); |
| 438 | shift = p - 32; |
| 439 | } |
| 440 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 441 | RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, int reg_lo, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 442 | int lit, bool is_div) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 443 | LOG(FATAL) << "Unexpected use of GenDivRemLit for x86"; |
| 444 | return rl_dest; |
| 445 | } |
| 446 | |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 447 | RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src, |
| 448 | int imm, bool is_div) { |
| 449 | // Use a multiply (and fixup) to perform an int div/rem by a constant. |
| 450 | |
| 451 | // We have to use fixed registers, so flush all the temps. |
| 452 | FlushAllRegs(); |
| 453 | LockCallTemps(); // Prepare for explicit register usage. |
| 454 | |
| 455 | // Assume that the result will be in EDX. |
| 456 | RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed, |
| 457 | r2, INVALID_REG, INVALID_SREG, INVALID_SREG}; |
| 458 | |
| 459 | // handle 0x80000000 / -1 special case. |
| 460 | LIR *minint_branch = 0; |
| 461 | if (imm == -1) { |
| 462 | if (is_div) { |
| 463 | LoadValueDirectFixed(rl_src, r0); |
| 464 | OpRegImm(kOpCmp, r0, 0x80000000); |
| 465 | minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq); |
| 466 | |
| 467 | // for x != MIN_INT, x / -1 == -x. |
| 468 | NewLIR1(kX86Neg32R, r0); |
| 469 | |
| 470 | LIR* branch_around = NewLIR1(kX86Jmp8, 0); |
| 471 | // The target for cmp/jmp above. |
| 472 | minint_branch->target = NewLIR0(kPseudoTargetLabel); |
| 473 | // EAX already contains the right value (0x80000000), |
| 474 | branch_around->target = NewLIR0(kPseudoTargetLabel); |
| 475 | } else { |
| 476 | // x % -1 == 0. |
| 477 | LoadConstantNoClobber(r0, 0); |
| 478 | } |
| 479 | // For this case, return the result in EAX. |
| 480 | rl_result.low_reg = r0; |
| 481 | } else { |
| 482 | DCHECK(imm <= -2 || imm >= 2); |
| 483 | // Use H.S.Warren's Hacker's Delight Chapter 10 and |
| 484 | // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication. |
| 485 | int magic, shift; |
| 486 | CalculateMagicAndShift(imm, magic, shift); |
| 487 | |
| 488 | /* |
| 489 | * For imm >= 2, |
| 490 | * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0 |
| 491 | * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0. |
| 492 | * For imm <= -2, |
| 493 | * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0 |
| 494 | * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0. |
| 495 | * We implement this algorithm in the following way: |
| 496 | * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX |
| 497 | * 2. if imm > 0 and magic < 0, add numerator to EDX |
| 498 | * if imm < 0 and magic > 0, sub numerator from EDX |
| 499 | * 3. if S !=0, SAR S bits for EDX |
| 500 | * 4. add 1 to EDX if EDX < 0 |
| 501 | * 5. Thus, EDX is the quotient |
| 502 | */ |
| 503 | |
| 504 | // Numerator into EAX. |
| 505 | int numerator_reg = -1; |
| 506 | if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) { |
| 507 | // We will need the value later. |
| 508 | if (rl_src.location == kLocPhysReg) { |
| 509 | // We can use it directly. |
| 510 | DCHECK(rl_src.low_reg != r0 && rl_src.low_reg != r2); |
| 511 | numerator_reg = rl_src.low_reg; |
| 512 | } else { |
| 513 | LoadValueDirectFixed(rl_src, r1); |
| 514 | numerator_reg = r1; |
| 515 | } |
| 516 | OpRegCopy(r0, numerator_reg); |
| 517 | } else { |
| 518 | // Only need this once. Just put it into EAX. |
| 519 | LoadValueDirectFixed(rl_src, r0); |
| 520 | } |
| 521 | |
| 522 | // EDX = magic. |
| 523 | LoadConstantNoClobber(r2, magic); |
| 524 | |
| 525 | // EDX:EAX = magic & dividend. |
| 526 | NewLIR1(kX86Imul32DaR, r2); |
| 527 | |
| 528 | if (imm > 0 && magic < 0) { |
| 529 | // Add numerator to EDX. |
| 530 | DCHECK_NE(numerator_reg, -1); |
| 531 | NewLIR2(kX86Add32RR, r2, numerator_reg); |
| 532 | } else if (imm < 0 && magic > 0) { |
| 533 | DCHECK_NE(numerator_reg, -1); |
| 534 | NewLIR2(kX86Sub32RR, r2, numerator_reg); |
| 535 | } |
| 536 | |
| 537 | // Do we need the shift? |
| 538 | if (shift != 0) { |
| 539 | // Shift EDX by 'shift' bits. |
| 540 | NewLIR2(kX86Sar32RI, r2, shift); |
| 541 | } |
| 542 | |
| 543 | // Add 1 to EDX if EDX < 0. |
| 544 | |
| 545 | // Move EDX to EAX. |
| 546 | OpRegCopy(r0, r2); |
| 547 | |
| 548 | // Move sign bit to bit 0, zeroing the rest. |
| 549 | NewLIR2(kX86Shr32RI, r2, 31); |
| 550 | |
| 551 | // EDX = EDX + EAX. |
| 552 | NewLIR2(kX86Add32RR, r2, r0); |
| 553 | |
| 554 | // Quotient is in EDX. |
| 555 | if (!is_div) { |
| 556 | // We need to compute the remainder. |
| 557 | // Remainder is divisor - (quotient * imm). |
| 558 | DCHECK_NE(numerator_reg, -1); |
| 559 | OpRegCopy(r0, numerator_reg); |
| 560 | |
| 561 | // EAX = numerator * imm. |
| 562 | OpRegRegImm(kOpMul, r2, r2, imm); |
| 563 | |
| 564 | // EDX -= EAX. |
| 565 | NewLIR2(kX86Sub32RR, r0, r2); |
| 566 | |
| 567 | // For this case, return the result in EAX. |
| 568 | rl_result.low_reg = r0; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return rl_result; |
| 573 | } |
| 574 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 575 | RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, int reg_lo, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 576 | int reg_hi, bool is_div) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 577 | LOG(FATAL) << "Unexpected use of GenDivRem for x86"; |
| 578 | return rl_dest; |
| 579 | } |
| 580 | |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 581 | RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1, |
| 582 | RegLocation rl_src2, bool is_div, bool check_zero) { |
| 583 | // We have to use fixed registers, so flush all the temps. |
| 584 | FlushAllRegs(); |
| 585 | LockCallTemps(); // Prepare for explicit register usage. |
| 586 | |
| 587 | // Load LHS into EAX. |
| 588 | LoadValueDirectFixed(rl_src1, r0); |
| 589 | |
| 590 | // Load RHS into EBX. |
| 591 | LoadValueDirectFixed(rl_src2, r1); |
| 592 | |
| 593 | // Copy LHS sign bit into EDX. |
| 594 | NewLIR0(kx86Cdq32Da); |
| 595 | |
| 596 | if (check_zero) { |
| 597 | // Handle division by zero case. |
| 598 | GenImmedCheck(kCondEq, r1, 0, kThrowDivZero); |
| 599 | } |
| 600 | |
| 601 | // Have to catch 0x80000000/-1 case, or we will get an exception! |
| 602 | OpRegImm(kOpCmp, r1, -1); |
| 603 | LIR *minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe); |
| 604 | |
| 605 | // RHS is -1. |
| 606 | OpRegImm(kOpCmp, r0, 0x80000000); |
| 607 | LIR * minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe); |
| 608 | |
| 609 | // In 0x80000000/-1 case. |
| 610 | if (!is_div) { |
| 611 | // For DIV, EAX is already right. For REM, we need EDX 0. |
| 612 | LoadConstantNoClobber(r2, 0); |
| 613 | } |
| 614 | LIR* done = NewLIR1(kX86Jmp8, 0); |
| 615 | |
| 616 | // Expected case. |
| 617 | minus_one_branch->target = NewLIR0(kPseudoTargetLabel); |
| 618 | minint_branch->target = minus_one_branch->target; |
| 619 | NewLIR1(kX86Idivmod32DaR, r1); |
| 620 | done->target = NewLIR0(kPseudoTargetLabel); |
| 621 | |
| 622 | // Result is in EAX for div and EDX for rem. |
| 623 | RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed, |
| 624 | r0, INVALID_REG, INVALID_SREG, INVALID_SREG}; |
| 625 | if (!is_div) { |
| 626 | rl_result.low_reg = r2; |
| 627 | } |
| 628 | return rl_result; |
| 629 | } |
| 630 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 631 | bool X86Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 632 | DCHECK_EQ(cu_->instruction_set, kX86); |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 633 | |
| 634 | // Get the two arguments to the invoke and place them in GP registers. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 635 | RegLocation rl_src1 = info->args[0]; |
| 636 | RegLocation rl_src2 = info->args[1]; |
| 637 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 638 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 639 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 640 | RegLocation rl_dest = InlineTarget(info); |
| 641 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * If the result register is the same as the second element, then we need to be careful. |
| 645 | * The reason is that the first copy will inadvertently clobber the second element with |
| 646 | * the first one thus yielding the wrong result. Thus we do a swap in that case. |
| 647 | */ |
| 648 | if (rl_result.low_reg == rl_src2.low_reg) { |
| 649 | std::swap(rl_src1, rl_src2); |
| 650 | } |
| 651 | |
| 652 | // Pick the first integer as min/max. |
| 653 | OpRegCopy(rl_result.low_reg, rl_src1.low_reg); |
| 654 | |
| 655 | // If the integers are both in the same register, then there is nothing else to do |
| 656 | // because they are equal and we have already moved one into the result. |
| 657 | if (rl_src1.low_reg != rl_src2.low_reg) { |
| 658 | // It is possible we didn't pick correctly so do the actual comparison now. |
| 659 | OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg); |
| 660 | |
| 661 | // Conditionally move the other integer into the destination register. |
| 662 | ConditionCode condition_code = is_min ? kCondGt : kCondLt; |
| 663 | OpCondRegReg(kOpCmov, condition_code, rl_result.low_reg, rl_src2.low_reg); |
| 664 | } |
| 665 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 666 | StoreValue(rl_dest, rl_result); |
| 667 | return true; |
| 668 | } |
| 669 | |
Vladimir Marko | e508a20 | 2013-11-04 15:24:22 +0000 | [diff] [blame] | 670 | bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) { |
| 671 | RegLocation rl_src_address = info->args[0]; // long address |
| 672 | rl_src_address.wide = 0; // ignore high half in info->args[1] |
| 673 | RegLocation rl_dest = InlineTarget(info); |
| 674 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); |
| 675 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 676 | if (size == kLong) { |
| 677 | // Unaligned access is allowed on x86. |
| 678 | LoadBaseDispWide(rl_address.low_reg, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG); |
| 679 | StoreValueWide(rl_dest, rl_result); |
| 680 | } else { |
| 681 | DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord); |
| 682 | // Unaligned access is allowed on x86. |
| 683 | LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG); |
| 684 | StoreValue(rl_dest, rl_result); |
| 685 | } |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) { |
| 690 | RegLocation rl_src_address = info->args[0]; // long address |
| 691 | rl_src_address.wide = 0; // ignore high half in info->args[1] |
| 692 | RegLocation rl_src_value = info->args[2]; // [size] value |
| 693 | RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); |
| 694 | if (size == kLong) { |
| 695 | // Unaligned access is allowed on x86. |
| 696 | RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg); |
| 697 | StoreBaseDispWide(rl_address.low_reg, 0, rl_value.low_reg, rl_value.high_reg); |
| 698 | } else { |
| 699 | DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord); |
| 700 | // Unaligned access is allowed on x86. |
| 701 | RegLocation rl_value = LoadValue(rl_src_value, kCoreReg); |
| 702 | StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size); |
| 703 | } |
| 704 | return true; |
| 705 | } |
| 706 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 707 | void X86Mir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 708 | NewLIR5(kX86Lea32RA, rBase, reg1, reg2, scale, offset); |
| 709 | } |
| 710 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 711 | void X86Mir2Lir::OpTlsCmp(ThreadOffset offset, int val) { |
| 712 | NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Vladimir Marko | 1c282e2 | 2013-11-21 14:49:47 +0000 | [diff] [blame] | 715 | bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) { |
Vladimir Marko | c29bb61 | 2013-11-27 16:47:25 +0000 | [diff] [blame] | 716 | DCHECK_EQ(cu_->instruction_set, kX86); |
| 717 | // Unused - RegLocation rl_src_unsafe = info->args[0]; |
| 718 | RegLocation rl_src_obj = info->args[1]; // Object - known non-null |
| 719 | RegLocation rl_src_offset = info->args[2]; // long low |
| 720 | rl_src_offset.wide = 0; // ignore high half in info->args[3] |
| 721 | RegLocation rl_src_expected = info->args[4]; // int, long or Object |
| 722 | // If is_long, high half is in info->args[5] |
| 723 | RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object |
| 724 | // If is_long, high half is in info->args[7] |
| 725 | |
| 726 | if (is_long) { |
Vladimir Marko | 70b797d | 2013-12-03 15:25:24 +0000 | [diff] [blame] | 727 | FlushAllRegs(); |
| 728 | LockCallTemps(); |
Vladimir Marko | a6fd8ba | 2013-12-13 10:53:49 +0000 | [diff] [blame] | 729 | LoadValueDirectWideFixed(rl_src_expected, rAX, rDX); |
| 730 | LoadValueDirectWideFixed(rl_src_new_value, rBX, rCX); |
Vladimir Marko | 70b797d | 2013-12-03 15:25:24 +0000 | [diff] [blame] | 731 | NewLIR1(kX86Push32R, rDI); |
| 732 | MarkTemp(rDI); |
| 733 | LockTemp(rDI); |
| 734 | NewLIR1(kX86Push32R, rSI); |
| 735 | MarkTemp(rSI); |
| 736 | LockTemp(rSI); |
Vladimir Marko | a6fd8ba | 2013-12-13 10:53:49 +0000 | [diff] [blame] | 737 | const int push_offset = 4 /* push edi */ + 4 /* push esi */; |
| 738 | LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_obj.s_reg_low) + push_offset, rDI); |
| 739 | LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_offset.s_reg_low) + push_offset, rSI); |
Vladimir Marko | 70b797d | 2013-12-03 15:25:24 +0000 | [diff] [blame] | 740 | NewLIR4(kX86LockCmpxchg8bA, rDI, rSI, 0, 0); |
| 741 | FreeTemp(rSI); |
| 742 | UnmarkTemp(rSI); |
| 743 | NewLIR1(kX86Pop32R, rSI); |
| 744 | FreeTemp(rDI); |
| 745 | UnmarkTemp(rDI); |
| 746 | NewLIR1(kX86Pop32R, rDI); |
| 747 | FreeCallTemps(); |
Vladimir Marko | c29bb61 | 2013-11-27 16:47:25 +0000 | [diff] [blame] | 748 | } else { |
| 749 | // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX. |
| 750 | FlushReg(r0); |
| 751 | LockTemp(r0); |
| 752 | |
| 753 | // Release store semantics, get the barrier out of the way. TODO: revisit |
| 754 | GenMemBarrier(kStoreLoad); |
| 755 | |
| 756 | RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg); |
| 757 | RegLocation rl_new_value = LoadValue(rl_src_new_value, kCoreReg); |
| 758 | |
| 759 | if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) { |
| 760 | // Mark card for object assuming new value is stored. |
| 761 | FreeTemp(r0); // Temporarily release EAX for MarkGCCard(). |
| 762 | MarkGCCard(rl_new_value.low_reg, rl_object.low_reg); |
| 763 | LockTemp(r0); |
| 764 | } |
| 765 | |
| 766 | RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg); |
| 767 | LoadValueDirect(rl_src_expected, r0); |
| 768 | NewLIR5(kX86LockCmpxchgAR, rl_object.low_reg, rl_offset.low_reg, 0, 0, rl_new_value.low_reg); |
| 769 | |
| 770 | FreeTemp(r0); |
| 771 | } |
| 772 | |
| 773 | // Convert ZF to boolean |
| 774 | RegLocation rl_dest = InlineTarget(info); // boolean place for result |
| 775 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 776 | NewLIR2(kX86Set8R, rl_result.low_reg, kX86CondZ); |
| 777 | NewLIR2(kX86Movzx8RR, rl_result.low_reg, rl_result.low_reg); |
| 778 | StoreValue(rl_dest, rl_result); |
| 779 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | LIR* X86Mir2Lir::OpPcRelLoad(int reg, LIR* target) { |
| 783 | LOG(FATAL) << "Unexpected use of OpPcRelLoad for x86"; |
| 784 | return NULL; |
| 785 | } |
| 786 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 787 | LIR* X86Mir2Lir::OpVldm(int rBase, int count) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 788 | LOG(FATAL) << "Unexpected use of OpVldm for x86"; |
| 789 | return NULL; |
| 790 | } |
| 791 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 792 | LIR* X86Mir2Lir::OpVstm(int rBase, int count) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 793 | LOG(FATAL) << "Unexpected use of OpVstm for x86"; |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src, |
| 798 | RegLocation rl_result, int lit, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 799 | int first_bit, int second_bit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 800 | int t_reg = AllocTemp(); |
| 801 | OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, second_bit - first_bit); |
| 802 | OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, t_reg); |
| 803 | FreeTemp(t_reg); |
| 804 | if (first_bit != 0) { |
| 805 | OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit); |
| 806 | } |
| 807 | } |
| 808 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 809 | void X86Mir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) { |
Razvan A Lupusoru | 090dd44 | 2013-12-20 14:35:03 -0800 | [diff] [blame] | 810 | // We are not supposed to clobber either of the provided registers, so allocate |
| 811 | // a temporary to use for the check. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 812 | int t_reg = AllocTemp(); |
Razvan A Lupusoru | 090dd44 | 2013-12-20 14:35:03 -0800 | [diff] [blame] | 813 | |
| 814 | // Doing an OR is a quick way to check if both registers are zero. This will set the flags. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 815 | OpRegRegReg(kOpOr, t_reg, reg_lo, reg_hi); |
Razvan A Lupusoru | 090dd44 | 2013-12-20 14:35:03 -0800 | [diff] [blame] | 816 | |
| 817 | // In case of zero, throw ArithmeticException. |
| 818 | GenCheck(kCondEq, kThrowDivZero); |
| 819 | |
| 820 | // The temp is no longer needed so free it at this time. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 821 | FreeTemp(t_reg); |
| 822 | } |
| 823 | |
| 824 | // Test suspend flag, return target of taken suspend branch |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 825 | LIR* X86Mir2Lir::OpTestSuspend(LIR* target) { |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 826 | OpTlsCmp(Thread::ThreadFlagsOffset(), 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 827 | return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target); |
| 828 | } |
| 829 | |
| 830 | // Decrement register and branch on condition |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 831 | LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 832 | OpRegImm(kOpSub, reg, 1); |
Yixin Shou | a0dac3e | 2014-01-23 05:01:22 -0800 | [diff] [blame] | 833 | return OpCondBranch(c_code, target); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 834 | } |
| 835 | |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 836 | bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 837 | RegLocation rl_src, RegLocation rl_dest, int lit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 838 | LOG(FATAL) << "Unexpected use of smallLiteralDive in x86"; |
| 839 | return false; |
| 840 | } |
| 841 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 842 | LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 843 | LOG(FATAL) << "Unexpected use of OpIT in x86"; |
| 844 | return NULL; |
| 845 | } |
| 846 | |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 847 | void X86Mir2Lir::GenImulRegImm(int dest, int src, int val) { |
| 848 | switch (val) { |
| 849 | case 0: |
| 850 | NewLIR2(kX86Xor32RR, dest, dest); |
| 851 | break; |
| 852 | case 1: |
| 853 | OpRegCopy(dest, src); |
| 854 | break; |
| 855 | default: |
| 856 | OpRegRegImm(kOpMul, dest, src, val); |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | void X86Mir2Lir::GenImulMemImm(int dest, int sreg, int displacement, int val) { |
| 862 | LIR *m; |
| 863 | switch (val) { |
| 864 | case 0: |
| 865 | NewLIR2(kX86Xor32RR, dest, dest); |
| 866 | break; |
| 867 | case 1: |
| 868 | LoadBaseDisp(rX86_SP, displacement, dest, kWord, sreg); |
| 869 | break; |
| 870 | default: |
| 871 | m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest, rX86_SP, |
| 872 | displacement, val); |
| 873 | AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */); |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 878 | void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 879 | RegLocation rl_src2) { |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 880 | if (rl_src1.is_const) { |
| 881 | std::swap(rl_src1, rl_src2); |
| 882 | } |
| 883 | // Are we multiplying by a constant? |
| 884 | if (rl_src2.is_const) { |
| 885 | // Do special compare/branch against simple const operand |
| 886 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 887 | if (val == 0) { |
| 888 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
| 889 | OpRegReg(kOpXor, rl_result.low_reg, rl_result.low_reg); |
| 890 | OpRegReg(kOpXor, rl_result.high_reg, rl_result.high_reg); |
| 891 | StoreValueWide(rl_dest, rl_result); |
| 892 | return; |
| 893 | } else if (val == 1) { |
| 894 | rl_src1 = EvalLocWide(rl_src1, kCoreReg, true); |
| 895 | StoreValueWide(rl_dest, rl_src1); |
| 896 | return; |
| 897 | } else if (val == 2) { |
| 898 | GenAddLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1); |
| 899 | return; |
| 900 | } else if (IsPowerOfTwo(val)) { |
| 901 | int shift_amount = LowestSetBit(val); |
| 902 | if (!BadOverlap(rl_src1, rl_dest)) { |
| 903 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 904 | RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, |
| 905 | rl_src1, shift_amount); |
| 906 | StoreValueWide(rl_dest, rl_result); |
| 907 | return; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // Okay, just bite the bullet and do it. |
| 912 | int32_t val_lo = Low32Bits(val); |
| 913 | int32_t val_hi = High32Bits(val); |
| 914 | FlushAllRegs(); |
| 915 | LockCallTemps(); // Prepare for explicit register usage. |
| 916 | rl_src1 = UpdateLocWide(rl_src1); |
| 917 | bool src1_in_reg = rl_src1.location == kLocPhysReg; |
| 918 | int displacement = SRegOffset(rl_src1.s_reg_low); |
| 919 | |
| 920 | // ECX <- 1H * 2L |
| 921 | // EAX <- 1L * 2H |
| 922 | if (src1_in_reg) { |
| 923 | GenImulRegImm(r1, rl_src1.high_reg, val_lo); |
| 924 | GenImulRegImm(r0, rl_src1.low_reg, val_hi); |
| 925 | } else { |
| 926 | GenImulMemImm(r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo); |
| 927 | GenImulMemImm(r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi); |
| 928 | } |
| 929 | |
| 930 | // ECX <- ECX + EAX (2H * 1L) + (1H * 2L) |
| 931 | NewLIR2(kX86Add32RR, r1, r0); |
| 932 | |
| 933 | // EAX <- 2L |
| 934 | LoadConstantNoClobber(r0, val_lo); |
| 935 | |
| 936 | // EDX:EAX <- 2L * 1L (double precision) |
| 937 | if (src1_in_reg) { |
| 938 | NewLIR1(kX86Mul32DaR, rl_src1.low_reg); |
| 939 | } else { |
| 940 | LIR *m = NewLIR2(kX86Mul32DaM, rX86_SP, displacement + LOWORD_OFFSET); |
| 941 | AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2, |
| 942 | true /* is_load */, true /* is_64bit */); |
| 943 | } |
| 944 | |
| 945 | // EDX <- EDX + ECX (add high words) |
| 946 | NewLIR2(kX86Add32RR, r2, r1); |
| 947 | |
| 948 | // Result is EDX:EAX |
| 949 | RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed, r0, r2, |
| 950 | INVALID_SREG, INVALID_SREG}; |
| 951 | StoreValueWide(rl_dest, rl_result); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | // Nope. Do it the hard way |
| 956 | FlushAllRegs(); |
| 957 | LockCallTemps(); // Prepare for explicit register usage. |
| 958 | rl_src1 = UpdateLocWide(rl_src1); |
| 959 | rl_src2 = UpdateLocWide(rl_src2); |
| 960 | |
| 961 | // At this point, the VRs are in their home locations. |
| 962 | bool src1_in_reg = rl_src1.location == kLocPhysReg; |
| 963 | bool src2_in_reg = rl_src2.location == kLocPhysReg; |
| 964 | |
| 965 | // ECX <- 1H |
| 966 | if (src1_in_reg) { |
| 967 | NewLIR2(kX86Mov32RR, r1, rl_src1.high_reg); |
| 968 | } else { |
| 969 | LoadBaseDisp(rX86_SP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, r1, |
| 970 | kWord, GetSRegHi(rl_src1.s_reg_low)); |
| 971 | } |
| 972 | |
| 973 | // EAX <- 2H |
| 974 | if (src2_in_reg) { |
| 975 | NewLIR2(kX86Mov32RR, r0, rl_src2.high_reg); |
| 976 | } else { |
| 977 | LoadBaseDisp(rX86_SP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, r0, |
| 978 | kWord, GetSRegHi(rl_src2.s_reg_low)); |
| 979 | } |
| 980 | |
| 981 | // EAX <- EAX * 1L (2H * 1L) |
| 982 | if (src1_in_reg) { |
| 983 | NewLIR2(kX86Imul32RR, r0, rl_src1.low_reg); |
| 984 | } else { |
| 985 | int displacement = SRegOffset(rl_src1.s_reg_low); |
| 986 | LIR *m = NewLIR3(kX86Imul32RM, r0, rX86_SP, displacement + LOWORD_OFFSET); |
| 987 | AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2, |
| 988 | true /* is_load */, true /* is_64bit */); |
| 989 | } |
| 990 | |
| 991 | // ECX <- ECX * 2L (1H * 2L) |
| 992 | if (src2_in_reg) { |
| 993 | NewLIR2(kX86Imul32RR, r1, rl_src2.low_reg); |
| 994 | } else { |
| 995 | int displacement = SRegOffset(rl_src2.s_reg_low); |
| 996 | LIR *m = NewLIR3(kX86Imul32RM, r1, rX86_SP, displacement + LOWORD_OFFSET); |
| 997 | AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2, |
| 998 | true /* is_load */, true /* is_64bit */); |
| 999 | } |
| 1000 | |
| 1001 | // ECX <- ECX + EAX (2H * 1L) + (1H * 2L) |
| 1002 | NewLIR2(kX86Add32RR, r1, r0); |
| 1003 | |
| 1004 | // EAX <- 2L |
| 1005 | if (src2_in_reg) { |
| 1006 | NewLIR2(kX86Mov32RR, r0, rl_src2.low_reg); |
| 1007 | } else { |
| 1008 | LoadBaseDisp(rX86_SP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, r0, |
| 1009 | kWord, rl_src2.s_reg_low); |
| 1010 | } |
| 1011 | |
| 1012 | // EDX:EAX <- 2L * 1L (double precision) |
| 1013 | if (src1_in_reg) { |
| 1014 | NewLIR1(kX86Mul32DaR, rl_src1.low_reg); |
| 1015 | } else { |
| 1016 | int displacement = SRegOffset(rl_src1.s_reg_low); |
| 1017 | LIR *m = NewLIR2(kX86Mul32DaM, rX86_SP, displacement + LOWORD_OFFSET); |
| 1018 | AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2, |
| 1019 | true /* is_load */, true /* is_64bit */); |
| 1020 | } |
| 1021 | |
| 1022 | // EDX <- EDX + ECX (add high words) |
| 1023 | NewLIR2(kX86Add32RR, r2, r1); |
| 1024 | |
| 1025 | // Result is EDX:EAX |
| 1026 | RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed, r0, r2, |
| 1027 | INVALID_SREG, INVALID_SREG}; |
| 1028 | StoreValueWide(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1029 | } |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1030 | |
| 1031 | void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src, |
| 1032 | Instruction::Code op) { |
| 1033 | DCHECK_EQ(rl_dest.location, kLocPhysReg); |
| 1034 | X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false); |
| 1035 | if (rl_src.location == kLocPhysReg) { |
| 1036 | // Both operands are in registers. |
| 1037 | if (rl_dest.low_reg == rl_src.high_reg) { |
| 1038 | // The registers are the same, so we would clobber it before the use. |
| 1039 | int temp_reg = AllocTemp(); |
| 1040 | OpRegCopy(temp_reg, rl_dest.low_reg); |
| 1041 | rl_src.high_reg = temp_reg; |
| 1042 | } |
| 1043 | NewLIR2(x86op, rl_dest.low_reg, rl_src.low_reg); |
| 1044 | |
| 1045 | x86op = GetOpcode(op, rl_dest, rl_src, true); |
| 1046 | NewLIR2(x86op, rl_dest.high_reg, rl_src.high_reg); |
| 1047 | FreeTemp(rl_src.low_reg); |
| 1048 | FreeTemp(rl_src.high_reg); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | // RHS is in memory. |
| 1053 | DCHECK((rl_src.location == kLocDalvikFrame) || |
| 1054 | (rl_src.location == kLocCompilerTemp)); |
| 1055 | int rBase = TargetReg(kSp); |
| 1056 | int displacement = SRegOffset(rl_src.s_reg_low); |
| 1057 | |
| 1058 | LIR *lir = NewLIR3(x86op, rl_dest.low_reg, rBase, displacement + LOWORD_OFFSET); |
| 1059 | AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2, |
| 1060 | true /* is_load */, true /* is64bit */); |
| 1061 | x86op = GetOpcode(op, rl_dest, rl_src, true); |
| 1062 | lir = NewLIR3(x86op, rl_dest.high_reg, rBase, displacement + HIWORD_OFFSET); |
| 1063 | AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2, |
| 1064 | true /* is_load */, true /* is64bit */); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1067 | void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) { |
| 1068 | rl_dest = UpdateLocWide(rl_dest); |
| 1069 | if (rl_dest.location == kLocPhysReg) { |
| 1070 | // Ensure we are in a register pair |
| 1071 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
| 1072 | |
| 1073 | rl_src = UpdateLocWide(rl_src); |
| 1074 | GenLongRegOrMemOp(rl_result, rl_src, op); |
| 1075 | StoreFinalValueWide(rl_dest, rl_result); |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | // It wasn't in registers, so it better be in memory. |
| 1080 | DCHECK((rl_dest.location == kLocDalvikFrame) || |
| 1081 | (rl_dest.location == kLocCompilerTemp)); |
| 1082 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 1083 | |
| 1084 | // Operate directly into memory. |
| 1085 | X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false); |
| 1086 | int rBase = TargetReg(kSp); |
| 1087 | int displacement = SRegOffset(rl_dest.s_reg_low); |
| 1088 | |
| 1089 | LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, rl_src.low_reg); |
| 1090 | AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2, |
| 1091 | false /* is_load */, true /* is64bit */); |
| 1092 | x86op = GetOpcode(op, rl_dest, rl_src, true); |
| 1093 | lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, rl_src.high_reg); |
| 1094 | AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2, |
| 1095 | false /* is_load */, true /* is64bit */); |
| 1096 | FreeTemp(rl_src.low_reg); |
| 1097 | FreeTemp(rl_src.high_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1100 | void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1, |
| 1101 | RegLocation rl_src2, Instruction::Code op, |
| 1102 | bool is_commutative) { |
| 1103 | // Is this really a 2 operand operation? |
| 1104 | switch (op) { |
| 1105 | case Instruction::ADD_LONG_2ADDR: |
| 1106 | case Instruction::SUB_LONG_2ADDR: |
| 1107 | case Instruction::AND_LONG_2ADDR: |
| 1108 | case Instruction::OR_LONG_2ADDR: |
| 1109 | case Instruction::XOR_LONG_2ADDR: |
| 1110 | GenLongArith(rl_dest, rl_src2, op); |
| 1111 | return; |
| 1112 | default: |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | if (rl_dest.location == kLocPhysReg) { |
| 1117 | RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg); |
| 1118 | |
| 1119 | // We are about to clobber the LHS, so it needs to be a temp. |
| 1120 | rl_result = ForceTempWide(rl_result); |
| 1121 | |
| 1122 | // Perform the operation using the RHS. |
| 1123 | rl_src2 = UpdateLocWide(rl_src2); |
| 1124 | GenLongRegOrMemOp(rl_result, rl_src2, op); |
| 1125 | |
| 1126 | // And now record that the result is in the temp. |
| 1127 | StoreFinalValueWide(rl_dest, rl_result); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | // It wasn't in registers, so it better be in memory. |
| 1132 | DCHECK((rl_dest.location == kLocDalvikFrame) || |
| 1133 | (rl_dest.location == kLocCompilerTemp)); |
| 1134 | rl_src1 = UpdateLocWide(rl_src1); |
| 1135 | rl_src2 = UpdateLocWide(rl_src2); |
| 1136 | |
| 1137 | // Get one of the source operands into temporary register. |
| 1138 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1139 | if (IsTemp(rl_src1.low_reg) && IsTemp(rl_src1.high_reg)) { |
| 1140 | GenLongRegOrMemOp(rl_src1, rl_src2, op); |
| 1141 | } else if (is_commutative) { |
| 1142 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 1143 | // We need at least one of them to be a temporary. |
| 1144 | if (!(IsTemp(rl_src2.low_reg) && IsTemp(rl_src2.high_reg))) { |
| 1145 | rl_src1 = ForceTempWide(rl_src1); |
| 1146 | } |
| 1147 | GenLongRegOrMemOp(rl_src1, rl_src2, op); |
| 1148 | } else { |
| 1149 | // Need LHS to be the temp. |
| 1150 | rl_src1 = ForceTempWide(rl_src1); |
| 1151 | GenLongRegOrMemOp(rl_src1, rl_src2, op); |
| 1152 | } |
| 1153 | |
| 1154 | StoreFinalValueWide(rl_dest, rl_src1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1157 | void X86Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1158 | RegLocation rl_src1, RegLocation rl_src2) { |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1159 | GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true); |
| 1160 | } |
| 1161 | |
| 1162 | void X86Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1163 | RegLocation rl_src1, RegLocation rl_src2) { |
| 1164 | GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false); |
| 1165 | } |
| 1166 | |
| 1167 | void X86Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1168 | RegLocation rl_src1, RegLocation rl_src2) { |
| 1169 | GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true); |
| 1170 | } |
| 1171 | |
| 1172 | void X86Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1173 | RegLocation rl_src1, RegLocation rl_src2) { |
| 1174 | GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true); |
| 1175 | } |
| 1176 | |
| 1177 | void X86Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1178 | RegLocation rl_src1, RegLocation rl_src2) { |
| 1179 | GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1182 | void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) { |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1183 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 1184 | RegLocation rl_result = ForceTempWide(rl_src); |
| 1185 | if (rl_dest.low_reg == rl_src.high_reg) { |
| 1186 | // The registers are the same, so we would clobber it before the use. |
| 1187 | int temp_reg = AllocTemp(); |
| 1188 | OpRegCopy(temp_reg, rl_result.low_reg); |
| 1189 | rl_result.high_reg = temp_reg; |
| 1190 | } |
| 1191 | OpRegReg(kOpNeg, rl_result.low_reg, rl_result.low_reg); // rLow = -rLow |
| 1192 | OpRegImm(kOpAdc, rl_result.high_reg, 0); // rHigh = rHigh + CF |
| 1193 | OpRegReg(kOpNeg, rl_result.high_reg, rl_result.high_reg); // rHigh = -rHigh |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1194 | StoreValueWide(rl_dest, rl_result); |
| 1195 | } |
| 1196 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1197 | void X86Mir2Lir::OpRegThreadMem(OpKind op, int r_dest, ThreadOffset thread_offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1198 | X86OpCode opcode = kX86Bkpt; |
| 1199 | switch (op) { |
| 1200 | case kOpCmp: opcode = kX86Cmp32RT; break; |
| 1201 | case kOpMov: opcode = kX86Mov32RT; break; |
| 1202 | default: |
| 1203 | LOG(FATAL) << "Bad opcode: " << op; |
| 1204 | break; |
| 1205 | } |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1206 | NewLIR2(opcode, r_dest, thread_offset.Int32Value()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Generate array load |
| 1211 | */ |
| 1212 | void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1213 | RegLocation rl_index, RegLocation rl_dest, int scale) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1214 | RegisterClass reg_class = oat_reg_class_by_size(size); |
| 1215 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1216 | RegLocation rl_result; |
| 1217 | rl_array = LoadValue(rl_array, kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1218 | |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1219 | int data_offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1220 | if (size == kLong || size == kDouble) { |
| 1221 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1222 | } else { |
| 1223 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1224 | } |
| 1225 | |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1226 | bool constant_index = rl_index.is_const; |
| 1227 | int32_t constant_index_value = 0; |
| 1228 | if (!constant_index) { |
| 1229 | rl_index = LoadValue(rl_index, kCoreReg); |
| 1230 | } else { |
| 1231 | constant_index_value = mir_graph_->ConstantValue(rl_index); |
| 1232 | // If index is constant, just fold it into the data offset |
| 1233 | data_offset += constant_index_value << scale; |
| 1234 | // treat as non array below |
| 1235 | rl_index.low_reg = INVALID_REG; |
| 1236 | } |
| 1237 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1238 | /* null object? */ |
| 1239 | GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags); |
| 1240 | |
| 1241 | if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) { |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1242 | if (constant_index) { |
| 1243 | GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset, |
| 1244 | constant_index_value, kThrowConstantArrayBounds); |
| 1245 | } else { |
| 1246 | GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg, |
| 1247 | len_offset, kThrowArrayBounds); |
| 1248 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1249 | } |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1250 | rl_result = EvalLoc(rl_dest, reg_class, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1251 | if ((size == kLong) || (size == kDouble)) { |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1252 | LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_result.low_reg, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1253 | rl_result.high_reg, size, INVALID_SREG); |
| 1254 | StoreValueWide(rl_dest, rl_result); |
| 1255 | } else { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1256 | LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, |
| 1257 | data_offset, rl_result.low_reg, INVALID_REG, size, |
| 1258 | INVALID_SREG); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1259 | StoreValue(rl_dest, rl_result); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * Generate array store |
| 1265 | * |
| 1266 | */ |
| 1267 | void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array, |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1268 | RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1269 | RegisterClass reg_class = oat_reg_class_by_size(size); |
| 1270 | int len_offset = mirror::Array::LengthOffset().Int32Value(); |
| 1271 | int data_offset; |
| 1272 | |
| 1273 | if (size == kLong || size == kDouble) { |
| 1274 | data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 1275 | } else { |
| 1276 | data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 1277 | } |
| 1278 | |
| 1279 | rl_array = LoadValue(rl_array, kCoreReg); |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1280 | bool constant_index = rl_index.is_const; |
| 1281 | int32_t constant_index_value = 0; |
| 1282 | if (!constant_index) { |
| 1283 | rl_index = LoadValue(rl_index, kCoreReg); |
| 1284 | } else { |
| 1285 | // If index is constant, just fold it into the data offset |
| 1286 | constant_index_value = mir_graph_->ConstantValue(rl_index); |
| 1287 | data_offset += constant_index_value << scale; |
| 1288 | // treat as non array below |
| 1289 | rl_index.low_reg = INVALID_REG; |
| 1290 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1291 | |
| 1292 | /* null object? */ |
| 1293 | GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags); |
| 1294 | |
| 1295 | if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) { |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1296 | if (constant_index) { |
| 1297 | GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset, |
| 1298 | constant_index_value, kThrowConstantArrayBounds); |
| 1299 | } else { |
| 1300 | GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg, |
| 1301 | len_offset, kThrowArrayBounds); |
| 1302 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1303 | } |
| 1304 | if ((size == kLong) || (size == kDouble)) { |
| 1305 | rl_src = LoadValueWide(rl_src, reg_class); |
| 1306 | } else { |
| 1307 | rl_src = LoadValue(rl_src, reg_class); |
| 1308 | } |
| 1309 | // If the src reg can't be byte accessed, move it to a temp first. |
| 1310 | if ((size == kSignedByte || size == kUnsignedByte) && rl_src.low_reg >= 4) { |
| 1311 | int temp = AllocTemp(); |
| 1312 | OpRegCopy(temp, rl_src.low_reg); |
| 1313 | StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, temp, |
| 1314 | INVALID_REG, size, INVALID_SREG); |
| 1315 | } else { |
| 1316 | StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_src.low_reg, |
| 1317 | rl_src.high_reg, size, INVALID_SREG); |
| 1318 | } |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1319 | if (card_mark) { |
Ian Rogers | 773aab1 | 2013-10-14 13:50:10 -0700 | [diff] [blame] | 1320 | // Free rl_index if its a temp. Ensures there are 2 free regs for card mark. |
Mark Mendell | 343adb5 | 2013-12-18 06:02:17 -0800 | [diff] [blame] | 1321 | if (!constant_index) { |
| 1322 | FreeTemp(rl_index.low_reg); |
| 1323 | } |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1324 | MarkGCCard(rl_src.low_reg, rl_array.low_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 1328 | RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1329 | RegLocation rl_src, int shift_amount) { |
| 1330 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1331 | switch (opcode) { |
| 1332 | case Instruction::SHL_LONG: |
| 1333 | case Instruction::SHL_LONG_2ADDR: |
| 1334 | DCHECK_NE(shift_amount, 1); // Prevent a double store from happening. |
| 1335 | if (shift_amount == 32) { |
| 1336 | OpRegCopy(rl_result.high_reg, rl_src.low_reg); |
| 1337 | LoadConstant(rl_result.low_reg, 0); |
| 1338 | } else if (shift_amount > 31) { |
| 1339 | OpRegCopy(rl_result.high_reg, rl_src.low_reg); |
| 1340 | FreeTemp(rl_src.high_reg); |
| 1341 | NewLIR2(kX86Sal32RI, rl_result.high_reg, shift_amount - 32); |
| 1342 | LoadConstant(rl_result.low_reg, 0); |
| 1343 | } else { |
| 1344 | OpRegCopy(rl_result.low_reg, rl_src.low_reg); |
| 1345 | OpRegCopy(rl_result.high_reg, rl_src.high_reg); |
| 1346 | NewLIR3(kX86Shld32RRI, rl_result.high_reg, rl_result.low_reg, shift_amount); |
| 1347 | NewLIR2(kX86Sal32RI, rl_result.low_reg, shift_amount); |
| 1348 | } |
| 1349 | break; |
| 1350 | case Instruction::SHR_LONG: |
| 1351 | case Instruction::SHR_LONG_2ADDR: |
| 1352 | if (shift_amount == 32) { |
| 1353 | OpRegCopy(rl_result.low_reg, rl_src.high_reg); |
| 1354 | OpRegCopy(rl_result.high_reg, rl_src.high_reg); |
| 1355 | NewLIR2(kX86Sar32RI, rl_result.high_reg, 31); |
| 1356 | } else if (shift_amount > 31) { |
| 1357 | OpRegCopy(rl_result.low_reg, rl_src.high_reg); |
| 1358 | OpRegCopy(rl_result.high_reg, rl_src.high_reg); |
| 1359 | NewLIR2(kX86Sar32RI, rl_result.low_reg, shift_amount - 32); |
| 1360 | NewLIR2(kX86Sar32RI, rl_result.high_reg, 31); |
| 1361 | } else { |
| 1362 | OpRegCopy(rl_result.low_reg, rl_src.low_reg); |
| 1363 | OpRegCopy(rl_result.high_reg, rl_src.high_reg); |
| 1364 | NewLIR3(kX86Shrd32RRI, rl_result.low_reg, rl_result.high_reg, shift_amount); |
| 1365 | NewLIR2(kX86Sar32RI, rl_result.high_reg, shift_amount); |
| 1366 | } |
| 1367 | break; |
| 1368 | case Instruction::USHR_LONG: |
| 1369 | case Instruction::USHR_LONG_2ADDR: |
| 1370 | if (shift_amount == 32) { |
| 1371 | OpRegCopy(rl_result.low_reg, rl_src.high_reg); |
| 1372 | LoadConstant(rl_result.high_reg, 0); |
| 1373 | } else if (shift_amount > 31) { |
| 1374 | OpRegCopy(rl_result.low_reg, rl_src.high_reg); |
| 1375 | NewLIR2(kX86Shr32RI, rl_result.low_reg, shift_amount - 32); |
| 1376 | LoadConstant(rl_result.high_reg, 0); |
| 1377 | } else { |
| 1378 | OpRegCopy(rl_result.low_reg, rl_src.low_reg); |
| 1379 | OpRegCopy(rl_result.high_reg, rl_src.high_reg); |
| 1380 | NewLIR3(kX86Shrd32RRI, rl_result.low_reg, rl_result.high_reg, shift_amount); |
| 1381 | NewLIR2(kX86Shr32RI, rl_result.high_reg, shift_amount); |
| 1382 | } |
| 1383 | break; |
| 1384 | default: |
| 1385 | LOG(FATAL) << "Unexpected case"; |
| 1386 | } |
| 1387 | return rl_result; |
| 1388 | } |
| 1389 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1390 | void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest, |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 1391 | RegLocation rl_src, RegLocation rl_shift) { |
| 1392 | // Per spec, we only care about low 6 bits of shift amount. |
| 1393 | int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f; |
| 1394 | if (shift_amount == 0) { |
| 1395 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 1396 | StoreValueWide(rl_dest, rl_src); |
| 1397 | return; |
| 1398 | } else if (shift_amount == 1 && |
| 1399 | (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) { |
| 1400 | // Need to handle this here to avoid calling StoreValueWide twice. |
| 1401 | GenAddLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src); |
| 1402 | return; |
| 1403 | } |
| 1404 | if (BadOverlap(rl_src, rl_dest)) { |
| 1405 | GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift); |
| 1406 | return; |
| 1407 | } |
| 1408 | rl_src = LoadValueWide(rl_src, kCoreReg); |
| 1409 | RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount); |
| 1410 | StoreValueWide(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1414 | RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) { |
Mark Mendell | e02d48f | 2014-01-15 11:19:23 -0800 | [diff] [blame] | 1415 | switch (opcode) { |
| 1416 | case Instruction::ADD_LONG: |
| 1417 | case Instruction::AND_LONG: |
| 1418 | case Instruction::OR_LONG: |
| 1419 | case Instruction::XOR_LONG: |
| 1420 | if (rl_src2.is_const) { |
| 1421 | GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode); |
| 1422 | } else { |
| 1423 | DCHECK(rl_src1.is_const); |
| 1424 | GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode); |
| 1425 | } |
| 1426 | break; |
| 1427 | case Instruction::SUB_LONG: |
| 1428 | case Instruction::SUB_LONG_2ADDR: |
| 1429 | if (rl_src2.is_const) { |
| 1430 | GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode); |
| 1431 | } else { |
| 1432 | GenSubLong(opcode, rl_dest, rl_src1, rl_src2); |
| 1433 | } |
| 1434 | break; |
| 1435 | case Instruction::ADD_LONG_2ADDR: |
| 1436 | case Instruction::OR_LONG_2ADDR: |
| 1437 | case Instruction::XOR_LONG_2ADDR: |
| 1438 | case Instruction::AND_LONG_2ADDR: |
| 1439 | if (rl_src2.is_const) { |
| 1440 | GenLongImm(rl_dest, rl_src2, opcode); |
| 1441 | } else { |
| 1442 | DCHECK(rl_src1.is_const); |
| 1443 | GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode); |
| 1444 | } |
| 1445 | break; |
| 1446 | default: |
| 1447 | // Default - bail to non-const handler. |
| 1448 | GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2); |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) { |
| 1454 | switch (op) { |
| 1455 | case Instruction::AND_LONG_2ADDR: |
| 1456 | case Instruction::AND_LONG: |
| 1457 | return value == -1; |
| 1458 | case Instruction::OR_LONG: |
| 1459 | case Instruction::OR_LONG_2ADDR: |
| 1460 | case Instruction::XOR_LONG: |
| 1461 | case Instruction::XOR_LONG_2ADDR: |
| 1462 | return value == 0; |
| 1463 | default: |
| 1464 | return false; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs, |
| 1469 | bool is_high_op) { |
| 1470 | bool rhs_in_mem = rhs.location != kLocPhysReg; |
| 1471 | bool dest_in_mem = dest.location != kLocPhysReg; |
| 1472 | DCHECK(!rhs_in_mem || !dest_in_mem); |
| 1473 | switch (op) { |
| 1474 | case Instruction::ADD_LONG: |
| 1475 | case Instruction::ADD_LONG_2ADDR: |
| 1476 | if (dest_in_mem) { |
| 1477 | return is_high_op ? kX86Adc32MR : kX86Add32MR; |
| 1478 | } else if (rhs_in_mem) { |
| 1479 | return is_high_op ? kX86Adc32RM : kX86Add32RM; |
| 1480 | } |
| 1481 | return is_high_op ? kX86Adc32RR : kX86Add32RR; |
| 1482 | case Instruction::SUB_LONG: |
| 1483 | case Instruction::SUB_LONG_2ADDR: |
| 1484 | if (dest_in_mem) { |
| 1485 | return is_high_op ? kX86Sbb32MR : kX86Sub32MR; |
| 1486 | } else if (rhs_in_mem) { |
| 1487 | return is_high_op ? kX86Sbb32RM : kX86Sub32RM; |
| 1488 | } |
| 1489 | return is_high_op ? kX86Sbb32RR : kX86Sub32RR; |
| 1490 | case Instruction::AND_LONG_2ADDR: |
| 1491 | case Instruction::AND_LONG: |
| 1492 | if (dest_in_mem) { |
| 1493 | return kX86And32MR; |
| 1494 | } |
| 1495 | return rhs_in_mem ? kX86And32RM : kX86And32RR; |
| 1496 | case Instruction::OR_LONG: |
| 1497 | case Instruction::OR_LONG_2ADDR: |
| 1498 | if (dest_in_mem) { |
| 1499 | return kX86Or32MR; |
| 1500 | } |
| 1501 | return rhs_in_mem ? kX86Or32RM : kX86Or32RR; |
| 1502 | case Instruction::XOR_LONG: |
| 1503 | case Instruction::XOR_LONG_2ADDR: |
| 1504 | if (dest_in_mem) { |
| 1505 | return kX86Xor32MR; |
| 1506 | } |
| 1507 | return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR; |
| 1508 | default: |
| 1509 | LOG(FATAL) << "Unexpected opcode: " << op; |
| 1510 | return kX86Add32RR; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op, |
| 1515 | int32_t value) { |
| 1516 | bool in_mem = loc.location != kLocPhysReg; |
| 1517 | bool byte_imm = IS_SIMM8(value); |
| 1518 | DCHECK(in_mem || !IsFpReg(loc.low_reg)); |
| 1519 | switch (op) { |
| 1520 | case Instruction::ADD_LONG: |
| 1521 | case Instruction::ADD_LONG_2ADDR: |
| 1522 | if (byte_imm) { |
| 1523 | if (in_mem) { |
| 1524 | return is_high_op ? kX86Adc32MI8 : kX86Add32MI8; |
| 1525 | } |
| 1526 | return is_high_op ? kX86Adc32RI8 : kX86Add32RI8; |
| 1527 | } |
| 1528 | if (in_mem) { |
| 1529 | return is_high_op ? kX86Adc32MI : kX86Add32MI; |
| 1530 | } |
| 1531 | return is_high_op ? kX86Adc32RI : kX86Add32RI; |
| 1532 | case Instruction::SUB_LONG: |
| 1533 | case Instruction::SUB_LONG_2ADDR: |
| 1534 | if (byte_imm) { |
| 1535 | if (in_mem) { |
| 1536 | return is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8; |
| 1537 | } |
| 1538 | return is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8; |
| 1539 | } |
| 1540 | if (in_mem) { |
| 1541 | return is_high_op ? kX86Sbb32MI : kX86Sub32MI; |
| 1542 | } |
| 1543 | return is_high_op ? kX86Sbb32RI : kX86Sub32RI; |
| 1544 | case Instruction::AND_LONG_2ADDR: |
| 1545 | case Instruction::AND_LONG: |
| 1546 | if (byte_imm) { |
| 1547 | return in_mem ? kX86And32MI8 : kX86And32RI8; |
| 1548 | } |
| 1549 | return in_mem ? kX86And32MI : kX86And32RI; |
| 1550 | case Instruction::OR_LONG: |
| 1551 | case Instruction::OR_LONG_2ADDR: |
| 1552 | if (byte_imm) { |
| 1553 | return in_mem ? kX86Or32MI8 : kX86Or32RI8; |
| 1554 | } |
| 1555 | return in_mem ? kX86Or32MI : kX86Or32RI; |
| 1556 | case Instruction::XOR_LONG: |
| 1557 | case Instruction::XOR_LONG_2ADDR: |
| 1558 | if (byte_imm) { |
| 1559 | return in_mem ? kX86Xor32MI8 : kX86Xor32RI8; |
| 1560 | } |
| 1561 | return in_mem ? kX86Xor32MI : kX86Xor32RI; |
| 1562 | default: |
| 1563 | LOG(FATAL) << "Unexpected opcode: " << op; |
| 1564 | return kX86Add32MI; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | void X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) { |
| 1569 | DCHECK(rl_src.is_const); |
| 1570 | int64_t val = mir_graph_->ConstantValueWide(rl_src); |
| 1571 | int32_t val_lo = Low32Bits(val); |
| 1572 | int32_t val_hi = High32Bits(val); |
| 1573 | rl_dest = UpdateLocWide(rl_dest); |
| 1574 | |
| 1575 | // Can we just do this into memory? |
| 1576 | if ((rl_dest.location == kLocDalvikFrame) || |
| 1577 | (rl_dest.location == kLocCompilerTemp)) { |
| 1578 | int rBase = TargetReg(kSp); |
| 1579 | int displacement = SRegOffset(rl_dest.s_reg_low); |
| 1580 | |
| 1581 | if (!IsNoOp(op, val_lo)) { |
| 1582 | X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo); |
| 1583 | LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, val_lo); |
| 1584 | AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2, |
| 1585 | false /* is_load */, true /* is64bit */); |
| 1586 | } |
| 1587 | if (!IsNoOp(op, val_hi)) { |
| 1588 | X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi); |
| 1589 | LIR *lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, val_hi); |
| 1590 | AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2, |
| 1591 | false /* is_load */, true /* is64bit */); |
| 1592 | } |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true); |
| 1597 | DCHECK_EQ(rl_result.location, kLocPhysReg); |
| 1598 | DCHECK(!IsFpReg(rl_result.low_reg)); |
| 1599 | |
| 1600 | if (!IsNoOp(op, val_lo)) { |
| 1601 | X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo); |
| 1602 | NewLIR2(x86op, rl_result.low_reg, val_lo); |
| 1603 | } |
| 1604 | if (!IsNoOp(op, val_hi)) { |
| 1605 | X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi); |
| 1606 | NewLIR2(x86op, rl_result.high_reg, val_hi); |
| 1607 | } |
| 1608 | StoreValueWide(rl_dest, rl_result); |
| 1609 | } |
| 1610 | |
| 1611 | void X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1, |
| 1612 | RegLocation rl_src2, Instruction::Code op) { |
| 1613 | DCHECK(rl_src2.is_const); |
| 1614 | int64_t val = mir_graph_->ConstantValueWide(rl_src2); |
| 1615 | int32_t val_lo = Low32Bits(val); |
| 1616 | int32_t val_hi = High32Bits(val); |
| 1617 | rl_dest = UpdateLocWide(rl_dest); |
| 1618 | rl_src1 = UpdateLocWide(rl_src1); |
| 1619 | |
| 1620 | // Can we do this directly into the destination registers? |
| 1621 | if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg && |
| 1622 | rl_dest.low_reg == rl_src1.low_reg && rl_dest.high_reg == rl_src1.high_reg && |
| 1623 | !IsFpReg(rl_dest.low_reg)) { |
| 1624 | if (!IsNoOp(op, val_lo)) { |
| 1625 | X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo); |
| 1626 | NewLIR2(x86op, rl_dest.low_reg, val_lo); |
| 1627 | } |
| 1628 | if (!IsNoOp(op, val_hi)) { |
| 1629 | X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi); |
| 1630 | NewLIR2(x86op, rl_dest.high_reg, val_hi); |
| 1631 | } |
| 1632 | return; |
| 1633 | } |
| 1634 | |
| 1635 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1636 | DCHECK_EQ(rl_src1.location, kLocPhysReg); |
| 1637 | |
| 1638 | // We need the values to be in a temporary |
| 1639 | RegLocation rl_result = ForceTempWide(rl_src1); |
| 1640 | if (!IsNoOp(op, val_lo)) { |
| 1641 | X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo); |
| 1642 | NewLIR2(x86op, rl_result.low_reg, val_lo); |
| 1643 | } |
| 1644 | if (!IsNoOp(op, val_hi)) { |
| 1645 | X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi); |
| 1646 | NewLIR2(x86op, rl_result.high_reg, val_hi); |
| 1647 | } |
| 1648 | |
| 1649 | StoreFinalValueWide(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Mark Mendell | df8ee2e | 2014-01-27 16:37:47 -0800 | [diff] [blame] | 1652 | // For final classes there are no sub-classes to check and so we can answer the instance-of |
| 1653 | // question with simple comparisons. Use compares to memory and SETEQ to optimize for x86. |
| 1654 | void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, |
| 1655 | RegLocation rl_dest, RegLocation rl_src) { |
| 1656 | RegLocation object = LoadValue(rl_src, kCoreReg); |
| 1657 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1658 | int result_reg = rl_result.low_reg; |
| 1659 | |
| 1660 | // SETcc only works with EAX..EDX. |
| 1661 | if (result_reg == object.low_reg || result_reg >= 4) { |
| 1662 | result_reg = AllocTypedTemp(false, kCoreReg); |
| 1663 | DCHECK_LT(result_reg, 4); |
| 1664 | } |
| 1665 | |
| 1666 | // Assume that there is no match. |
| 1667 | LoadConstant(result_reg, 0); |
| 1668 | LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL); |
| 1669 | |
| 1670 | int check_class = AllocTypedTemp(false, kCoreReg); |
| 1671 | |
| 1672 | // If Method* is already in a register, we can save a copy. |
| 1673 | RegLocation rl_method = mir_graph_->GetMethodLoc(); |
| 1674 | int32_t offset_of_type = mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + |
| 1675 | (sizeof(mirror::Class*) * type_idx); |
| 1676 | |
| 1677 | if (rl_method.location == kLocPhysReg) { |
| 1678 | if (use_declaring_class) { |
| 1679 | LoadWordDisp(rl_method.low_reg, |
| 1680 | mirror::ArtMethod::DeclaringClassOffset().Int32Value(), |
| 1681 | check_class); |
| 1682 | } else { |
| 1683 | LoadWordDisp(rl_method.low_reg, |
| 1684 | mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), |
| 1685 | check_class); |
| 1686 | LoadWordDisp(check_class, offset_of_type, check_class); |
| 1687 | } |
| 1688 | } else { |
| 1689 | LoadCurrMethodDirect(check_class); |
| 1690 | if (use_declaring_class) { |
| 1691 | LoadWordDisp(check_class, |
| 1692 | mirror::ArtMethod::DeclaringClassOffset().Int32Value(), |
| 1693 | check_class); |
| 1694 | } else { |
| 1695 | LoadWordDisp(check_class, |
| 1696 | mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), |
| 1697 | check_class); |
| 1698 | LoadWordDisp(check_class, offset_of_type, check_class); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | // Compare the computed class to the class in the object. |
| 1703 | DCHECK_EQ(object.location, kLocPhysReg); |
| 1704 | OpRegMem(kOpCmp, check_class, object.low_reg, |
| 1705 | mirror::Object::ClassOffset().Int32Value()); |
| 1706 | |
| 1707 | // Set the low byte of the result to 0 or 1 from the compare condition code. |
| 1708 | NewLIR2(kX86Set8R, result_reg, kX86CondEq); |
| 1709 | |
| 1710 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 1711 | null_branchover->target = target; |
| 1712 | FreeTemp(check_class); |
| 1713 | if (IsTemp(result_reg)) { |
| 1714 | OpRegCopy(rl_result.low_reg, result_reg); |
| 1715 | FreeTemp(result_reg); |
| 1716 | } |
| 1717 | StoreValue(rl_dest, rl_result); |
| 1718 | } |
| 1719 | |
Mark Mendell | 6607d97 | 2014-02-10 06:54:18 -0800 | [diff] [blame^] | 1720 | void X86Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final, |
| 1721 | bool type_known_abstract, bool use_declaring_class, |
| 1722 | bool can_assume_type_is_in_dex_cache, |
| 1723 | uint32_t type_idx, RegLocation rl_dest, |
| 1724 | RegLocation rl_src) { |
| 1725 | FlushAllRegs(); |
| 1726 | // May generate a call - use explicit registers. |
| 1727 | LockCallTemps(); |
| 1728 | LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 gets current Method*. |
| 1729 | int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*. |
| 1730 | // Reference must end up in kArg0. |
| 1731 | if (needs_access_check) { |
| 1732 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 1733 | // Caller function returns Class* in kArg0. |
| 1734 | CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess), |
| 1735 | type_idx, true); |
| 1736 | OpRegCopy(class_reg, TargetReg(kRet0)); |
| 1737 | LoadValueDirectFixed(rl_src, TargetReg(kArg0)); |
| 1738 | } else if (use_declaring_class) { |
| 1739 | LoadValueDirectFixed(rl_src, TargetReg(kArg0)); |
| 1740 | LoadWordDisp(TargetReg(kArg1), |
| 1741 | mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg); |
| 1742 | } else { |
| 1743 | // Load dex cache entry into class_reg (kArg2). |
| 1744 | LoadValueDirectFixed(rl_src, TargetReg(kArg0)); |
| 1745 | LoadWordDisp(TargetReg(kArg1), |
| 1746 | mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg); |
| 1747 | int32_t offset_of_type = |
| 1748 | mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*) |
| 1749 | * type_idx); |
| 1750 | LoadWordDisp(class_reg, offset_of_type, class_reg); |
| 1751 | if (!can_assume_type_is_in_dex_cache) { |
| 1752 | // Need to test presence of type in dex cache at runtime. |
| 1753 | LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL); |
| 1754 | // Type is not resolved. Call out to helper, which will return resolved type in kRet0/kArg0. |
| 1755 | CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true); |
| 1756 | OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path. |
| 1757 | LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* Reload Ref. */ |
| 1758 | // Rejoin code paths |
| 1759 | LIR* hop_target = NewLIR0(kPseudoTargetLabel); |
| 1760 | hop_branch->target = hop_target; |
| 1761 | } |
| 1762 | } |
| 1763 | /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result. */ |
| 1764 | RegLocation rl_result = GetReturn(false); |
| 1765 | |
| 1766 | // SETcc only works with EAX..EDX. |
| 1767 | DCHECK_LT(rl_result.low_reg, 4); |
| 1768 | |
| 1769 | // Is the class NULL? |
| 1770 | LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL); |
| 1771 | |
| 1772 | /* Load object->klass_. */ |
| 1773 | DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0); |
| 1774 | LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1)); |
| 1775 | /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class. */ |
| 1776 | LIR* branchover = nullptr; |
| 1777 | if (type_known_final) { |
| 1778 | // Ensure top 3 bytes of result are 0. |
| 1779 | LoadConstant(rl_result.low_reg, 0); |
| 1780 | OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); |
| 1781 | // Set the low byte of the result to 0 or 1 from the compare condition code. |
| 1782 | NewLIR2(kX86Set8R, rl_result.low_reg, kX86CondEq); |
| 1783 | } else { |
| 1784 | if (!type_known_abstract) { |
| 1785 | LoadConstant(rl_result.low_reg, 1); // Assume result succeeds. |
| 1786 | branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL); |
| 1787 | } |
| 1788 | OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); |
| 1789 | OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial)); |
| 1790 | } |
| 1791 | // TODO: only clobber when type isn't final? |
| 1792 | ClobberCallerSave(); |
| 1793 | /* Branch targets here. */ |
| 1794 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 1795 | StoreValue(rl_dest, rl_result); |
| 1796 | branch1->target = target; |
| 1797 | if (branchover != nullptr) { |
| 1798 | branchover->target = target; |
| 1799 | } |
| 1800 | } |
| 1801 | |
Mark Mendell | feb2b4e | 2014-01-28 12:59:49 -0800 | [diff] [blame] | 1802 | void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest, |
| 1803 | RegLocation rl_lhs, RegLocation rl_rhs) { |
| 1804 | OpKind op = kOpBkpt; |
| 1805 | bool is_div_rem = false; |
| 1806 | bool unary = false; |
| 1807 | bool shift_op = false; |
| 1808 | bool is_two_addr = false; |
| 1809 | RegLocation rl_result; |
| 1810 | switch (opcode) { |
| 1811 | case Instruction::NEG_INT: |
| 1812 | op = kOpNeg; |
| 1813 | unary = true; |
| 1814 | break; |
| 1815 | case Instruction::NOT_INT: |
| 1816 | op = kOpMvn; |
| 1817 | unary = true; |
| 1818 | break; |
| 1819 | case Instruction::ADD_INT_2ADDR: |
| 1820 | is_two_addr = true; |
| 1821 | // Fallthrough |
| 1822 | case Instruction::ADD_INT: |
| 1823 | op = kOpAdd; |
| 1824 | break; |
| 1825 | case Instruction::SUB_INT_2ADDR: |
| 1826 | is_two_addr = true; |
| 1827 | // Fallthrough |
| 1828 | case Instruction::SUB_INT: |
| 1829 | op = kOpSub; |
| 1830 | break; |
| 1831 | case Instruction::MUL_INT_2ADDR: |
| 1832 | is_two_addr = true; |
| 1833 | // Fallthrough |
| 1834 | case Instruction::MUL_INT: |
| 1835 | op = kOpMul; |
| 1836 | break; |
| 1837 | case Instruction::DIV_INT_2ADDR: |
| 1838 | is_two_addr = true; |
| 1839 | // Fallthrough |
| 1840 | case Instruction::DIV_INT: |
| 1841 | op = kOpDiv; |
| 1842 | is_div_rem = true; |
| 1843 | break; |
| 1844 | /* NOTE: returns in kArg1 */ |
| 1845 | case Instruction::REM_INT_2ADDR: |
| 1846 | is_two_addr = true; |
| 1847 | // Fallthrough |
| 1848 | case Instruction::REM_INT: |
| 1849 | op = kOpRem; |
| 1850 | is_div_rem = true; |
| 1851 | break; |
| 1852 | case Instruction::AND_INT_2ADDR: |
| 1853 | is_two_addr = true; |
| 1854 | // Fallthrough |
| 1855 | case Instruction::AND_INT: |
| 1856 | op = kOpAnd; |
| 1857 | break; |
| 1858 | case Instruction::OR_INT_2ADDR: |
| 1859 | is_two_addr = true; |
| 1860 | // Fallthrough |
| 1861 | case Instruction::OR_INT: |
| 1862 | op = kOpOr; |
| 1863 | break; |
| 1864 | case Instruction::XOR_INT_2ADDR: |
| 1865 | is_two_addr = true; |
| 1866 | // Fallthrough |
| 1867 | case Instruction::XOR_INT: |
| 1868 | op = kOpXor; |
| 1869 | break; |
| 1870 | case Instruction::SHL_INT_2ADDR: |
| 1871 | is_two_addr = true; |
| 1872 | // Fallthrough |
| 1873 | case Instruction::SHL_INT: |
| 1874 | shift_op = true; |
| 1875 | op = kOpLsl; |
| 1876 | break; |
| 1877 | case Instruction::SHR_INT_2ADDR: |
| 1878 | is_two_addr = true; |
| 1879 | // Fallthrough |
| 1880 | case Instruction::SHR_INT: |
| 1881 | shift_op = true; |
| 1882 | op = kOpAsr; |
| 1883 | break; |
| 1884 | case Instruction::USHR_INT_2ADDR: |
| 1885 | is_two_addr = true; |
| 1886 | // Fallthrough |
| 1887 | case Instruction::USHR_INT: |
| 1888 | shift_op = true; |
| 1889 | op = kOpLsr; |
| 1890 | break; |
| 1891 | default: |
| 1892 | LOG(FATAL) << "Invalid word arith op: " << opcode; |
| 1893 | } |
| 1894 | |
| 1895 | // Can we convert to a two address instruction? |
| 1896 | if (!is_two_addr && |
| 1897 | (mir_graph_->SRegToVReg(rl_dest.s_reg_low) == |
| 1898 | mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) { |
| 1899 | is_two_addr = true; |
| 1900 | } |
| 1901 | |
| 1902 | // Get the div/rem stuff out of the way. |
| 1903 | if (is_div_rem) { |
| 1904 | rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, true); |
| 1905 | StoreValue(rl_dest, rl_result); |
| 1906 | return; |
| 1907 | } |
| 1908 | |
| 1909 | if (unary) { |
| 1910 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 1911 | rl_result = UpdateLoc(rl_dest); |
| 1912 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1913 | OpRegReg(op, rl_result.low_reg, rl_lhs.low_reg); |
| 1914 | } else { |
| 1915 | if (shift_op) { |
| 1916 | // X86 doesn't require masking and must use ECX. |
| 1917 | int t_reg = TargetReg(kCount); // rCX |
| 1918 | LoadValueDirectFixed(rl_rhs, t_reg); |
| 1919 | if (is_two_addr) { |
| 1920 | // Can we do this directly into memory? |
| 1921 | rl_result = UpdateLoc(rl_dest); |
| 1922 | rl_rhs = LoadValue(rl_rhs, kCoreReg); |
| 1923 | if (rl_result.location != kLocPhysReg) { |
| 1924 | // Okay, we can do this into memory |
| 1925 | OpMemReg(op, rl_result, t_reg); |
| 1926 | FreeTemp(t_reg); |
| 1927 | return; |
| 1928 | } else if (!IsFpReg(rl_result.low_reg)) { |
| 1929 | // Can do this directly into the result register |
| 1930 | OpRegReg(op, rl_result.low_reg, t_reg); |
| 1931 | FreeTemp(t_reg); |
| 1932 | StoreFinalValue(rl_dest, rl_result); |
| 1933 | return; |
| 1934 | } |
| 1935 | } |
| 1936 | // Three address form, or we can't do directly. |
| 1937 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 1938 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1939 | OpRegRegReg(op, rl_result.low_reg, rl_lhs.low_reg, t_reg); |
| 1940 | FreeTemp(t_reg); |
| 1941 | } else { |
| 1942 | // Multiply is 3 operand only (sort of). |
| 1943 | if (is_two_addr && op != kOpMul) { |
| 1944 | // Can we do this directly into memory? |
| 1945 | rl_result = UpdateLoc(rl_dest); |
| 1946 | if (rl_result.location == kLocPhysReg) { |
| 1947 | // Can we do this from memory directly? |
| 1948 | rl_rhs = UpdateLoc(rl_rhs); |
| 1949 | if (rl_rhs.location != kLocPhysReg) { |
| 1950 | OpRegMem(op, rl_result.low_reg, rl_rhs); |
| 1951 | StoreFinalValue(rl_dest, rl_result); |
| 1952 | return; |
| 1953 | } else if (!IsFpReg(rl_rhs.low_reg)) { |
| 1954 | OpRegReg(op, rl_result.low_reg, rl_rhs.low_reg); |
| 1955 | StoreFinalValue(rl_dest, rl_result); |
| 1956 | return; |
| 1957 | } |
| 1958 | } |
| 1959 | rl_rhs = LoadValue(rl_rhs, kCoreReg); |
| 1960 | if (rl_result.location != kLocPhysReg) { |
| 1961 | // Okay, we can do this into memory. |
| 1962 | OpMemReg(op, rl_result, rl_rhs.low_reg); |
| 1963 | return; |
| 1964 | } else if (!IsFpReg(rl_result.low_reg)) { |
| 1965 | // Can do this directly into the result register. |
| 1966 | OpRegReg(op, rl_result.low_reg, rl_rhs.low_reg); |
| 1967 | StoreFinalValue(rl_dest, rl_result); |
| 1968 | return; |
| 1969 | } else { |
| 1970 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 1971 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1972 | OpRegRegReg(op, rl_result.low_reg, rl_lhs.low_reg, rl_rhs.low_reg); |
| 1973 | } |
| 1974 | } else { |
| 1975 | // Try to use reg/memory instructions. |
| 1976 | rl_lhs = UpdateLoc(rl_lhs); |
| 1977 | rl_rhs = UpdateLoc(rl_rhs); |
| 1978 | // We can't optimize with FP registers. |
| 1979 | if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) { |
| 1980 | // Something is difficult, so fall back to the standard case. |
| 1981 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 1982 | rl_rhs = LoadValue(rl_rhs, kCoreReg); |
| 1983 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1984 | OpRegRegReg(op, rl_result.low_reg, rl_lhs.low_reg, rl_rhs.low_reg); |
| 1985 | } else { |
| 1986 | // We can optimize by moving to result and using memory operands. |
| 1987 | if (rl_rhs.location != kLocPhysReg) { |
| 1988 | // Force LHS into result. |
| 1989 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1990 | LoadValueDirect(rl_lhs, rl_result.low_reg); |
| 1991 | OpRegMem(op, rl_result.low_reg, rl_rhs); |
| 1992 | } else if (rl_lhs.location != kLocPhysReg) { |
| 1993 | // RHS is in a register; LHS is in memory. |
| 1994 | if (op != kOpSub) { |
| 1995 | // Force RHS into result and operate on memory. |
| 1996 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1997 | OpRegCopy(rl_result.low_reg, rl_rhs.low_reg); |
| 1998 | OpRegMem(op, rl_result.low_reg, rl_lhs); |
| 1999 | } else { |
| 2000 | // Subtraction isn't commutative. |
| 2001 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 2002 | rl_rhs = LoadValue(rl_rhs, kCoreReg); |
| 2003 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 2004 | OpRegRegReg(op, rl_result.low_reg, rl_lhs.low_reg, rl_rhs.low_reg); |
| 2005 | } |
| 2006 | } else { |
| 2007 | // Both are in registers. |
| 2008 | rl_lhs = LoadValue(rl_lhs, kCoreReg); |
| 2009 | rl_rhs = LoadValue(rl_rhs, kCoreReg); |
| 2010 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 2011 | OpRegRegReg(op, rl_result.low_reg, rl_lhs.low_reg, rl_rhs.low_reg); |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | StoreValue(rl_dest, rl_result); |
| 2018 | } |
| 2019 | |
| 2020 | bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) { |
| 2021 | // If we have non-core registers, then we can't do good things. |
| 2022 | if (rl_lhs.location == kLocPhysReg && IsFpReg(rl_lhs.low_reg)) { |
| 2023 | return false; |
| 2024 | } |
| 2025 | if (rl_rhs.location == kLocPhysReg && IsFpReg(rl_rhs.low_reg)) { |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | // Everything will be fine :-). |
| 2030 | return true; |
| 2031 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2032 | } // namespace art |