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