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 | */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 16 | #include "dex/compiler_ir.h" |
| 17 | #include "dex/compiler_internals.h" |
Brian Carlstrom | 60d7a65 | 2014-03-13 18:10:08 -0700 | [diff] [blame] | 18 | #include "dex/quick/arm/arm_lir.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 20 | #include "entrypoints/quick/quick_entrypoints.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | #include "mirror/array.h" |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 22 | #include "mirror/object_array-inl.h" |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 23 | #include "mirror/object-inl.h" |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 24 | #include "mirror/object_reference.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include "verifier/method_verifier.h" |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 26 | #include <functional> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 30 | // Shortcuts to repeatedly used long types. |
| 31 | typedef mirror::ObjectArray<mirror::Object> ObjArray; |
| 32 | typedef mirror::ObjectArray<mirror::Class> ClassArray; |
| 33 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | /* |
| 35 | * This source files contains "gen" codegen routines that should |
| 36 | * be applicable to most targets. Only mid-level support utilities |
| 37 | * and "op" calls may be used here. |
| 38 | */ |
| 39 | |
| 40 | /* |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 41 | * Generate a kPseudoBarrier marker to indicate the boundary of special |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | * blocks. |
| 43 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 44 | void Mir2Lir::GenBarrier() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | LIR* barrier = NewLIR0(kPseudoBarrier); |
| 46 | /* Mark all resources as being clobbered */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 47 | DCHECK(!barrier->flags.use_def_invalid); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 48 | barrier->u.m.def_mask = &kEncodeAll; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 51 | void Mir2Lir::GenDivZeroException() { |
| 52 | LIR* branch = OpUnconditionalBranch(nullptr); |
| 53 | AddDivZeroCheckSlowPath(branch); |
| 54 | } |
| 55 | |
| 56 | void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) { |
Mingyao Yang | 4289456 | 2014-04-07 12:42:16 -0700 | [diff] [blame] | 57 | LIR* branch = OpCondBranch(c_code, nullptr); |
| 58 | AddDivZeroCheckSlowPath(branch); |
| 59 | } |
| 60 | |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 61 | void Mir2Lir::GenDivZeroCheck(RegStorage reg) { |
| 62 | LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr); |
Mingyao Yang | 4289456 | 2014-04-07 12:42:16 -0700 | [diff] [blame] | 63 | AddDivZeroCheckSlowPath(branch); |
| 64 | } |
| 65 | |
| 66 | void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) { |
| 67 | class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath { |
| 68 | public: |
| 69 | DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch) |
| 70 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) { |
| 71 | } |
| 72 | |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 73 | void Compile() OVERRIDE { |
Mingyao Yang | 4289456 | 2014-04-07 12:42:16 -0700 | [diff] [blame] | 74 | m2l_->ResetRegPool(); |
| 75 | m2l_->ResetDefTracking(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 76 | GenerateTargetLabel(kPseudoThrowTarget); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 77 | m2l_->CallRuntimeHelper(kQuickThrowDivZero, true); |
Mingyao Yang | 4289456 | 2014-04-07 12:42:16 -0700 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | |
| 81 | AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch)); |
| 82 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 83 | |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 84 | void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) { |
| 85 | class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath { |
| 86 | public: |
| 87 | ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length) |
| 88 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch), |
| 89 | index_(index), length_(length) { |
| 90 | } |
| 91 | |
| 92 | void Compile() OVERRIDE { |
| 93 | m2l_->ResetRegPool(); |
| 94 | m2l_->ResetDefTracking(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 95 | GenerateTargetLabel(kPseudoThrowTarget); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 96 | m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true); |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private: |
| 100 | const RegStorage index_; |
| 101 | const RegStorage length_; |
| 102 | }; |
| 103 | |
| 104 | LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr); |
| 105 | AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length)); |
| 106 | } |
| 107 | |
| 108 | void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) { |
| 109 | class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath { |
| 110 | public: |
| 111 | ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length) |
| 112 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch), |
| 113 | index_(index), length_(length) { |
| 114 | } |
| 115 | |
| 116 | void Compile() OVERRIDE { |
| 117 | m2l_->ResetRegPool(); |
| 118 | m2l_->ResetDefTracking(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 119 | GenerateTargetLabel(kPseudoThrowTarget); |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 120 | |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 121 | RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide); |
| 122 | RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 123 | |
| 124 | m2l_->OpRegCopy(arg1_32, length_); |
| 125 | m2l_->LoadConstant(arg0_32, index_); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 126 | m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true); |
Mingyao Yang | 80365d9 | 2014-04-18 12:10:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
| 130 | const int32_t index_; |
| 131 | const RegStorage length_; |
| 132 | }; |
| 133 | |
| 134 | LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr); |
| 135 | AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length)); |
| 136 | } |
| 137 | |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 138 | LIR* Mir2Lir::GenNullCheck(RegStorage reg) { |
| 139 | class NullCheckSlowPath : public Mir2Lir::LIRSlowPath { |
| 140 | public: |
| 141 | NullCheckSlowPath(Mir2Lir* m2l, LIR* branch) |
| 142 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) { |
| 143 | } |
| 144 | |
| 145 | void Compile() OVERRIDE { |
| 146 | m2l_->ResetRegPool(); |
| 147 | m2l_->ResetDefTracking(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 148 | GenerateTargetLabel(kPseudoThrowTarget); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 149 | m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true); |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | |
| 153 | LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr); |
| 154 | AddSlowPath(new (arena_) NullCheckSlowPath(this, branch)); |
| 155 | return branch; |
| 156 | } |
| 157 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 158 | /* Perform null-check on a register. */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 159 | LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 160 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 161 | return GenExplicitNullCheck(m_reg, opt_flags); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 162 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 163 | return nullptr; |
| 164 | } |
| 165 | |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 166 | /* Perform an explicit null-check on a register. */ |
| 167 | LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) { |
| 168 | if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) { |
| 169 | return NULL; |
| 170 | } |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 171 | return GenNullCheck(m_reg); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 174 | void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 175 | if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 176 | if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) { |
| 177 | return; |
| 178 | } |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 179 | // Insert after last instruction. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 180 | MarkSafepointPC(last_lir_insn_); |
| 181 | } |
| 182 | } |
| 183 | |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 184 | void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 185 | if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 186 | if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) { |
| 187 | return; |
| 188 | } |
| 189 | MarkSafepointPCAfter(after); |
| 190 | } |
| 191 | } |
| 192 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 193 | void Mir2Lir::MarkPossibleStackOverflowException() { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 194 | if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 195 | MarkSafepointPC(last_lir_insn_); |
| 196 | } |
| 197 | } |
| 198 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 199 | void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 200 | if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 201 | if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) { |
| 202 | return; |
| 203 | } |
| 204 | // Force an implicit null check by performing a memory operation (load) from the given |
| 205 | // register with offset 0. This will cause a signal if the register contains 0 (null). |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 206 | RegStorage tmp = AllocTemp(); |
| 207 | // TODO: for Mips, would be best to use rZERO as the bogus register target. |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 208 | LIR* load = Load32Disp(reg, 0, tmp); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 209 | FreeTemp(tmp); |
| 210 | MarkSafepointPC(load); |
| 211 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 214 | void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1, |
| 215 | RegLocation rl_src2, LIR* taken, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 216 | LIR* fall_through) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | ConditionCode cond; |
buzbee | 7c02e91 | 2014-10-03 13:14:17 -0700 | [diff] [blame] | 218 | RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 219 | switch (opcode) { |
| 220 | case Instruction::IF_EQ: |
| 221 | cond = kCondEq; |
| 222 | break; |
| 223 | case Instruction::IF_NE: |
| 224 | cond = kCondNe; |
| 225 | break; |
| 226 | case Instruction::IF_LT: |
| 227 | cond = kCondLt; |
| 228 | break; |
| 229 | case Instruction::IF_GE: |
| 230 | cond = kCondGe; |
| 231 | break; |
| 232 | case Instruction::IF_GT: |
| 233 | cond = kCondGt; |
| 234 | break; |
| 235 | case Instruction::IF_LE: |
| 236 | cond = kCondLe; |
| 237 | break; |
| 238 | default: |
| 239 | cond = static_cast<ConditionCode>(0); |
| 240 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 241 | } |
| 242 | |
| 243 | // Normalize such that if either operand is constant, src2 will be constant |
| 244 | if (rl_src1.is_const) { |
| 245 | RegLocation rl_temp = rl_src1; |
| 246 | rl_src1 = rl_src2; |
| 247 | rl_src2 = rl_temp; |
| 248 | cond = FlipComparisonOrder(cond); |
| 249 | } |
| 250 | |
buzbee | 7c02e91 | 2014-10-03 13:14:17 -0700 | [diff] [blame] | 251 | rl_src1 = LoadValue(rl_src1, reg_class); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 252 | // Is this really an immediate comparison? |
| 253 | if (rl_src2.is_const) { |
| 254 | // If it's already live in a register or not easily materialized, just keep going |
| 255 | RegLocation rl_temp = UpdateLoc(rl_src2); |
Andreas Gampe | b07c1f9 | 2014-07-26 01:40:39 -0700 | [diff] [blame] | 256 | int32_t constant_value = mir_graph_->ConstantValue(rl_src2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 257 | if ((rl_temp.location == kLocDalvikFrame) && |
Matteo Franchin | c763e35 | 2014-07-04 12:53:27 +0100 | [diff] [blame] | 258 | InexpensiveConstantInt(constant_value, opcode)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 259 | // OK - convert this to a compare immediate and branch |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 260 | OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | return; |
| 262 | } |
Andreas Gampe | b07c1f9 | 2014-07-26 01:40:39 -0700 | [diff] [blame] | 263 | |
| 264 | // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse |
| 265 | // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register |
| 266 | // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses |
| 267 | // the 32b literal 0 for null. |
| 268 | if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) { |
| 269 | // Use the OpCmpImmBranch and ignore the value in the register. |
| 270 | OpCmpImmBranch(cond, rl_src1.reg, 0, taken); |
| 271 | return; |
| 272 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | } |
Andreas Gampe | b07c1f9 | 2014-07-26 01:40:39 -0700 | [diff] [blame] | 274 | |
buzbee | 7c02e91 | 2014-10-03 13:14:17 -0700 | [diff] [blame] | 275 | rl_src2 = LoadValue(rl_src2, reg_class); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 276 | OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 280 | LIR* fall_through) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 281 | ConditionCode cond; |
buzbee | 7c02e91 | 2014-10-03 13:14:17 -0700 | [diff] [blame] | 282 | RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg; |
| 283 | rl_src = LoadValue(rl_src, reg_class); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | switch (opcode) { |
| 285 | case Instruction::IF_EQZ: |
| 286 | cond = kCondEq; |
| 287 | break; |
| 288 | case Instruction::IF_NEZ: |
| 289 | cond = kCondNe; |
| 290 | break; |
| 291 | case Instruction::IF_LTZ: |
| 292 | cond = kCondLt; |
| 293 | break; |
| 294 | case Instruction::IF_GEZ: |
| 295 | cond = kCondGe; |
| 296 | break; |
| 297 | case Instruction::IF_GTZ: |
| 298 | cond = kCondGt; |
| 299 | break; |
| 300 | case Instruction::IF_LEZ: |
| 301 | cond = kCondLe; |
| 302 | break; |
| 303 | default: |
| 304 | cond = static_cast<ConditionCode>(0); |
| 305 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 306 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 307 | OpCmpImmBranch(cond, rl_src.reg, 0, taken); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 310 | void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 311 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 312 | if (rl_src.location == kLocPhysReg) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 313 | OpRegCopy(rl_result.reg, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 314 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 315 | LoadValueDirect(rl_src, rl_result.reg.GetLow()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 316 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 317 | OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 318 | StoreValueWide(rl_dest, rl_result); |
| 319 | } |
| 320 | |
| 321 | void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 322 | RegLocation rl_src) { |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 323 | rl_src = LoadValue(rl_src, kCoreReg); |
| 324 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 325 | OpKind op = kOpInvalid; |
| 326 | switch (opcode) { |
| 327 | case Instruction::INT_TO_BYTE: |
| 328 | op = kOp2Byte; |
| 329 | break; |
| 330 | case Instruction::INT_TO_SHORT: |
| 331 | op = kOp2Short; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 332 | break; |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 333 | case Instruction::INT_TO_CHAR: |
| 334 | op = kOp2Char; |
| 335 | break; |
| 336 | default: |
| 337 | LOG(ERROR) << "Bad int conversion type"; |
| 338 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 339 | OpRegReg(op, rl_result.reg, rl_src.reg); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 340 | StoreValue(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 343 | /* |
| 344 | * Let helper function take care of everything. Will call |
| 345 | * Array::AllocFromCode(type_idx, method, count); |
| 346 | * Note: AllocFromCode will handle checks for errNegativeArraySize. |
| 347 | */ |
| 348 | void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest, |
| 349 | RegLocation rl_src) { |
| 350 | FlushAllRegs(); /* Everything to home location */ |
| 351 | const DexFile* dex_file = cu_->dex_file; |
| 352 | CompilerDriver* driver = cu_->compiler_driver; |
| 353 | if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) { |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 354 | bool is_type_initialized; // Ignored as an array does not have an initializer. |
| 355 | bool use_direct_type_ptr; |
| 356 | uintptr_t direct_type_ptr; |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 357 | bool is_finalizable; |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 358 | if (kEmbedClassInCode && |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 359 | driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr, |
| 360 | &direct_type_ptr, &is_finalizable)) { |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 361 | // The fast path. |
| 362 | if (!use_direct_type_ptr) { |
Fred Shih | e7f82e2 | 2014-08-06 10:46:37 -0700 | [diff] [blame] | 363 | LoadClassType(*dex_file, type_idx, kArg0); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 364 | CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide), |
| 365 | rl_src, true); |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 366 | } else { |
| 367 | // Use the direct pointer. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 368 | CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src, |
| 369 | true); |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 370 | } |
| 371 | } else { |
| 372 | // The slow path. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 373 | CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true); |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 374 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 375 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 376 | CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 377 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 378 | StoreValue(rl_dest, GetReturn(kRefReg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Similar to GenNewArray, but with post-allocation initialization. |
| 383 | * Verifier guarantees we're dealing with an array class. Current |
| 384 | * code throws runtime exception "bad Filled array req" for 'D' and 'J'. |
| 385 | * Current code also throws internal unimp if not 'L', '[' or 'I'. |
| 386 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 387 | void Mir2Lir::GenFilledNewArray(CallInfo* info) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 388 | int elems = info->num_arg_words; |
| 389 | int type_idx = info->index; |
| 390 | FlushAllRegs(); /* Everything to home location */ |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 391 | QuickEntrypointEnum target; |
| 392 | if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file, |
| 393 | type_idx)) { |
| 394 | target = kQuickCheckAndAllocArray; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 395 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 396 | target = kQuickCheckAndAllocArrayWithAccessCheck; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 398 | CallRuntimeHelperImmMethodImm(target, type_idx, elems, true); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 399 | FreeTemp(TargetReg(kArg2, kNotWide)); |
| 400 | FreeTemp(TargetReg(kArg1, kNotWide)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 401 | /* |
| 402 | * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the |
| 403 | * return region. Because AllocFromCode placed the new array |
| 404 | * in kRet0, we'll just lock it into place. When debugger support is |
| 405 | * added, it may be necessary to additionally copy all return |
| 406 | * values to a home location in thread-local storage |
| 407 | */ |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 408 | RegStorage ref_reg = TargetReg(kRet0, kRef); |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 409 | LockTemp(ref_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 410 | |
| 411 | // TODO: use the correct component size, currently all supported types |
| 412 | // share array alignment with ints (see comment at head of function) |
| 413 | size_t component_size = sizeof(int32_t); |
| 414 | |
| 415 | // Having a range of 0 is legal |
| 416 | if (info->is_range && (elems > 0)) { |
| 417 | /* |
| 418 | * Bit of ugliness here. We're going generate a mem copy loop |
| 419 | * on the register range, but it is possible that some regs |
| 420 | * in the range have been promoted. This is unlikely, but |
| 421 | * before generating the copy, we'll just force a flush |
| 422 | * of any regs in the source range that have been promoted to |
| 423 | * home location. |
| 424 | */ |
| 425 | for (int i = 0; i < elems; i++) { |
| 426 | RegLocation loc = UpdateLoc(info->args[i]); |
| 427 | if (loc.location == kLocPhysReg) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 428 | ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg); |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 429 | Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | /* |
| 433 | * TUNING note: generated code here could be much improved, but |
| 434 | * this is an uncommon operation and isn't especially performance |
| 435 | * critical. |
| 436 | */ |
Chao-ying Fu | 7e399fd | 2014-06-10 18:11:11 -0700 | [diff] [blame] | 437 | // This is addressing the stack, which may be out of the 4G area. |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 438 | RegStorage r_src = AllocTempRef(); |
| 439 | RegStorage r_dst = AllocTempRef(); |
| 440 | RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 441 | RegStorage r_val; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 442 | switch (cu_->instruction_set) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 443 | case kThumb2: |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 444 | case kArm64: |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 445 | r_val = TargetReg(kLr, kNotWide); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 446 | break; |
| 447 | case kX86: |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 448 | case kX86_64: |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 449 | FreeTemp(ref_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 450 | r_val = AllocTemp(); |
| 451 | break; |
| 452 | case kMips: |
| 453 | r_val = AllocTemp(); |
| 454 | break; |
| 455 | default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set; |
| 456 | } |
| 457 | // Set up source pointer |
| 458 | RegLocation rl_first = info->args[0]; |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 459 | OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 460 | // Set up the target pointer |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 461 | OpRegRegImm(kOpAdd, r_dst, ref_reg, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 462 | mirror::Array::DataOffset(component_size).Int32Value()); |
| 463 | // Set up the loop counter (known to be > 0) |
| 464 | LoadConstant(r_idx, elems - 1); |
| 465 | // Generate the copy loop. Going backwards for convenience |
| 466 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 467 | // Copy next element |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 468 | { |
| 469 | ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg); |
| 470 | LoadBaseIndexed(r_src, r_idx, r_val, 2, k32); |
| 471 | // NOTE: No dalvik register annotation, local optimizations will be stopped |
| 472 | // by the loop boundaries. |
| 473 | } |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 474 | StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 475 | FreeTemp(r_val); |
| 476 | OpDecAndBranch(kCondGe, r_idx, target); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 477 | if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 478 | // Restore the target pointer |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 479 | OpRegRegImm(kOpAdd, ref_reg, r_dst, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 480 | -mirror::Array::DataOffset(component_size).Int32Value()); |
| 481 | } |
| 482 | } else if (!info->is_range) { |
| 483 | // TUNING: interleave |
| 484 | for (int i = 0; i < elems; i++) { |
| 485 | RegLocation rl_arg = LoadValue(info->args[i], kCoreReg); |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 486 | Store32Disp(ref_reg, |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 487 | mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 488 | // If the LoadValue caused a temp to be allocated, free it |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 489 | if (IsTemp(rl_arg.reg)) { |
| 490 | FreeTemp(rl_arg.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
| 494 | if (info->result.location != kLocInvalid) { |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 495 | StoreValue(info->result, GetReturn(kRefReg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Ian Rogers | 832336b | 2014-10-08 15:35:22 -0700 | [diff] [blame] | 499 | /* |
| 500 | * Array data table format: |
| 501 | * ushort ident = 0x0300 magic value |
| 502 | * ushort width width of each element in the table |
| 503 | * uint size number of elements in the table |
| 504 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 505 | * padding at the end) |
| 506 | * |
| 507 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 508 | */ |
| 509 | void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
| 510 | if (kIsDebugBuild) { |
| 511 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
| 512 | const Instruction::ArrayDataPayload* payload = |
| 513 | reinterpret_cast<const Instruction::ArrayDataPayload*>(table); |
| 514 | CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature)); |
| 515 | } |
| 516 | uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset); |
| 517 | CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true); |
| 518 | } |
| 519 | |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 520 | // |
| 521 | // Slow path to ensure a class is initialized for sget/sput. |
| 522 | // |
| 523 | class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath { |
| 524 | public: |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 525 | StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index, |
| 526 | RegStorage r_base) : |
| 527 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), |
| 528 | storage_index_(storage_index), r_base_(r_base) { |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void Compile() { |
| 532 | LIR* unresolved_target = GenerateTargetLabel(); |
| 533 | uninit_->target = unresolved_target; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 534 | m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 535 | // Copy helper's result into r_base, a no-op on all but MIPS. |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 536 | m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef)); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 537 | |
| 538 | m2l_->OpUnconditionalBranch(cont_); |
| 539 | } |
| 540 | |
| 541 | private: |
| 542 | LIR* const uninit_; |
| 543 | const int storage_index_; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 544 | const RegStorage r_base_; |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 545 | }; |
| 546 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 547 | void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 548 | const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir); |
| 549 | cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass()); |
Douglas Leung | d9cb8ae | 2014-07-09 14:28:35 -0700 | [diff] [blame] | 550 | if (!SLOW_FIELD_PATH && field_info.FastPut()) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 551 | DCHECK_GE(field_info.FieldOffset().Int32Value(), 0); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 552 | RegStorage r_base; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 553 | if (field_info.IsReferrersClass()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 554 | // Fast path, static storage base is this method's class |
Matteo Franchin | 0955f7e | 2014-05-23 17:32:52 +0100 | [diff] [blame] | 555 | RegLocation rl_method = LoadCurrMethod(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 556 | r_base = AllocTempRef(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 557 | LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base, |
| 558 | kNotVolatile); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 559 | if (IsTemp(rl_method.reg)) { |
| 560 | FreeTemp(rl_method.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 561 | } |
| 562 | } else { |
| 563 | // Medium path, static storage base in a different class which requires checks that the other |
| 564 | // class is initialized. |
| 565 | // TODO: remove initialized check now that we are initializing classes in the compiler driver. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 566 | DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 567 | // May do runtime call so everything to home locations. |
| 568 | FlushAllRegs(); |
| 569 | // Using fixed register to sync with possible call to runtime support. |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 570 | RegStorage r_method = TargetReg(kArg1, kRef); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 571 | LockTemp(r_method); |
| 572 | LoadCurrMethodDirect(r_method); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 573 | r_base = TargetReg(kArg0, kRef); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 574 | LockTemp(r_base); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 575 | LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base, |
| 576 | kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 577 | int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 578 | LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 579 | // r_base now points at static storage (Class*) or NULL if the type is not yet resolved. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 580 | if (!field_info.IsInitialized() && |
| 581 | (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) { |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 582 | // Check if r_base is NULL or a not yet initialized class. |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 583 | |
| 584 | // The slow path is invoked if the r_base is NULL or the class pointed |
| 585 | // to by it is not initialized. |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 586 | LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 587 | RegStorage r_tmp = TargetReg(kArg2, kNotWide); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 588 | LockTemp(r_tmp); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 589 | LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base, |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 590 | mirror::Class::StatusOffset().Int32Value(), |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 591 | mirror::Class::kStatusInitialized, nullptr, nullptr); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 592 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 593 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 594 | AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 595 | field_info.StorageIndex(), r_base)); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 596 | |
| 597 | FreeTemp(r_tmp); |
Hans Boehm | 48f5c47 | 2014-06-27 14:50:10 -0700 | [diff] [blame] | 598 | // Ensure load of status and store of value don't re-order. |
| 599 | // TODO: Presumably the actual value store is control-dependent on the status load, |
| 600 | // and will thus not be reordered in any case, since stores are never speculated. |
| 601 | // Does later code "know" that the class is now initialized? If so, we still |
| 602 | // need the barrier to guard later static loads. |
| 603 | GenMemBarrier(kLoadAny); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 604 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 605 | FreeTemp(r_method); |
| 606 | } |
| 607 | // rBase now holds static storage base |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 608 | RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile()); |
| 609 | if (IsWide(size)) { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 610 | rl_src = LoadValueWide(rl_src, reg_class); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 611 | } else { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 612 | rl_src = LoadValue(rl_src, reg_class); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 613 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 614 | if (IsRef(size)) { |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 615 | StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, |
| 616 | field_info.IsVolatile() ? kVolatile : kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 617 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 618 | StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size, |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 619 | field_info.IsVolatile() ? kVolatile : kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 620 | } |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 621 | if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 622 | MarkGCCard(rl_src.reg, r_base); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 623 | } |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 624 | FreeTemp(r_base); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 625 | } else { |
| 626 | FlushAllRegs(); // Everything to home locations |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 627 | QuickEntrypointEnum target; |
| 628 | switch (size) { |
| 629 | case kReference: |
| 630 | target = kQuickSetObjStatic; |
| 631 | break; |
| 632 | case k64: |
| 633 | case kDouble: |
| 634 | target = kQuickSet64Static; |
| 635 | break; |
| 636 | case k32: |
| 637 | case kSingle: |
| 638 | target = kQuickSet32Static; |
| 639 | break; |
| 640 | case kSignedHalf: |
| 641 | case kUnsignedHalf: |
| 642 | target = kQuickSet16Static; |
| 643 | break; |
| 644 | case kSignedByte: |
| 645 | case kUnsignedByte: |
| 646 | target = kQuickSet8Static; |
| 647 | break; |
| 648 | case kWord: // Intentional fallthrough. |
| 649 | default: |
| 650 | LOG(FATAL) << "Can't determine entrypoint for: " << size; |
| 651 | target = kQuickSet32Static; |
| 652 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 653 | CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 657 | void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 658 | const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir); |
| 659 | cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass()); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 660 | |
Douglas Leung | d9cb8ae | 2014-07-09 14:28:35 -0700 | [diff] [blame] | 661 | if (!SLOW_FIELD_PATH && field_info.FastGet()) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 662 | DCHECK_GE(field_info.FieldOffset().Int32Value(), 0); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 663 | RegStorage r_base; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 664 | if (field_info.IsReferrersClass()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 665 | // Fast path, static storage base is this method's class |
| 666 | RegLocation rl_method = LoadCurrMethod(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 667 | r_base = AllocTempRef(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 668 | LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base, |
| 669 | kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 670 | } else { |
| 671 | // Medium path, static storage base in a different class which requires checks that the other |
| 672 | // class is initialized |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 673 | DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 674 | // May do runtime call so everything to home locations. |
| 675 | FlushAllRegs(); |
| 676 | // Using fixed register to sync with possible call to runtime support. |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 677 | RegStorage r_method = TargetReg(kArg1, kRef); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 678 | LockTemp(r_method); |
| 679 | LoadCurrMethodDirect(r_method); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 680 | r_base = TargetReg(kArg0, kRef); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 681 | LockTemp(r_base); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 682 | LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base, |
| 683 | kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 684 | int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 685 | LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 686 | // r_base now points at static storage (Class*) or NULL if the type is not yet resolved. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 687 | if (!field_info.IsInitialized() && |
| 688 | (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) { |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 689 | // Check if r_base is NULL or a not yet initialized class. |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 690 | |
| 691 | // The slow path is invoked if the r_base is NULL or the class pointed |
| 692 | // to by it is not initialized. |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 693 | LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 694 | RegStorage r_tmp = TargetReg(kArg2, kNotWide); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 695 | LockTemp(r_tmp); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 696 | LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base, |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 697 | mirror::Class::StatusOffset().Int32Value(), |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 698 | mirror::Class::kStatusInitialized, nullptr, nullptr); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 699 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 700 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 701 | AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 702 | field_info.StorageIndex(), r_base)); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 703 | |
| 704 | FreeTemp(r_tmp); |
Ian Rogers | 03dbc04 | 2014-06-02 14:24:56 -0700 | [diff] [blame] | 705 | // Ensure load of status and load of value don't re-order. |
Hans Boehm | 48f5c47 | 2014-06-27 14:50:10 -0700 | [diff] [blame] | 706 | GenMemBarrier(kLoadAny); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 707 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 708 | FreeTemp(r_method); |
| 709 | } |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 710 | // r_base now holds static storage base |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 711 | RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile()); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 712 | RegLocation rl_result = EvalLoc(rl_dest, reg_class, true); |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 713 | |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 714 | int field_offset = field_info.FieldOffset().Int32Value(); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 715 | if (IsRef(size)) { |
| 716 | // TODO: DCHECK? |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 717 | LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile : |
| 718 | kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 719 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 720 | LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ? |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 721 | kVolatile : kNotVolatile); |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 722 | } |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 723 | FreeTemp(r_base); |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 724 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 725 | if (IsWide(size)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 726 | StoreValueWide(rl_dest, rl_result); |
| 727 | } else { |
| 728 | StoreValue(rl_dest, rl_result); |
| 729 | } |
| 730 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 731 | DCHECK(SizeMatchesTypeForEntrypoint(size, type)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 732 | FlushAllRegs(); // Everything to home locations |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 733 | QuickEntrypointEnum target; |
| 734 | switch (type) { |
| 735 | case Primitive::kPrimNot: |
| 736 | target = kQuickGetObjStatic; |
| 737 | break; |
| 738 | case Primitive::kPrimLong: |
| 739 | case Primitive::kPrimDouble: |
| 740 | target = kQuickGet64Static; |
| 741 | break; |
| 742 | case Primitive::kPrimInt: |
| 743 | case Primitive::kPrimFloat: |
| 744 | target = kQuickGet32Static; |
| 745 | break; |
| 746 | case Primitive::kPrimShort: |
| 747 | target = kQuickGetShortStatic; |
| 748 | break; |
| 749 | case Primitive::kPrimChar: |
| 750 | target = kQuickGetCharStatic; |
| 751 | break; |
| 752 | case Primitive::kPrimByte: |
| 753 | target = kQuickGetByteStatic; |
| 754 | break; |
| 755 | case Primitive::kPrimBoolean: |
| 756 | target = kQuickGetBooleanStatic; |
| 757 | break; |
| 758 | case Primitive::kPrimVoid: // Intentional fallthrough. |
| 759 | default: |
| 760 | LOG(FATAL) << "Can't determine entrypoint for: " << type; |
| 761 | target = kQuickGet32Static; |
| 762 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 763 | CallRuntimeHelperImm(target, field_info.FieldIndex(), true); |
| 764 | |
Douglas Leung | 2db3e26 | 2014-06-25 16:02:55 -0700 | [diff] [blame] | 765 | // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp. |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 766 | if (IsWide(size)) { |
Douglas Leung | 2db3e26 | 2014-06-25 16:02:55 -0700 | [diff] [blame] | 767 | RegLocation rl_result = GetReturnWide(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 768 | StoreValueWide(rl_dest, rl_result); |
| 769 | } else { |
Douglas Leung | 2db3e26 | 2014-06-25 16:02:55 -0700 | [diff] [blame] | 770 | RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 771 | StoreValue(rl_dest, rl_result); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 776 | // Generate code for all slow paths. |
| 777 | void Mir2Lir::HandleSlowPaths() { |
Chao-ying Fu | 8159af6 | 2014-07-07 17:13:52 -0700 | [diff] [blame] | 778 | // We should check slow_paths_.Size() every time, because a new slow path |
| 779 | // may be created during slowpath->Compile(). |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 780 | for (LIRSlowPath* slowpath : slow_paths_) { |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 781 | slowpath->Compile(); |
| 782 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 783 | slow_paths_.clear(); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 786 | void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type, |
| 787 | RegLocation rl_dest, RegLocation rl_obj) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 788 | const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir); |
| 789 | cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet()); |
Douglas Leung | d9cb8ae | 2014-07-09 14:28:35 -0700 | [diff] [blame] | 790 | if (!SLOW_FIELD_PATH && field_info.FastGet()) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 791 | RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile()); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 792 | // A load of the class will lead to an iget with offset 0. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 793 | DCHECK_GE(field_info.FieldOffset().Int32Value(), 0); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 794 | rl_obj = LoadValue(rl_obj, kRefReg); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 795 | GenNullCheck(rl_obj.reg, opt_flags); |
| 796 | RegLocation rl_result = EvalLoc(rl_dest, reg_class, true); |
| 797 | int field_offset = field_info.FieldOffset().Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 798 | LIR* load_lir; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 799 | if (IsRef(size)) { |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 800 | load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ? |
| 801 | kVolatile : kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 802 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 803 | load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size, |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 804 | field_info.IsVolatile() ? kVolatile : kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 805 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 806 | MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 807 | if (IsWide(size)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 808 | StoreValueWide(rl_dest, rl_result); |
| 809 | } else { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 810 | StoreValue(rl_dest, rl_result); |
| 811 | } |
| 812 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 813 | DCHECK(SizeMatchesTypeForEntrypoint(size, type)); |
| 814 | QuickEntrypointEnum target; |
| 815 | switch (type) { |
| 816 | case Primitive::kPrimNot: |
| 817 | target = kQuickGetObjInstance; |
| 818 | break; |
| 819 | case Primitive::kPrimLong: |
| 820 | case Primitive::kPrimDouble: |
| 821 | target = kQuickGet64Instance; |
| 822 | break; |
| 823 | case Primitive::kPrimFloat: |
| 824 | case Primitive::kPrimInt: |
| 825 | target = kQuickGet32Instance; |
| 826 | break; |
| 827 | case Primitive::kPrimShort: |
| 828 | target = kQuickGetShortInstance; |
| 829 | break; |
| 830 | case Primitive::kPrimChar: |
| 831 | target = kQuickGetCharInstance; |
| 832 | break; |
| 833 | case Primitive::kPrimByte: |
| 834 | target = kQuickGetByteInstance; |
| 835 | break; |
| 836 | case Primitive::kPrimBoolean: |
| 837 | target = kQuickGetBooleanInstance; |
| 838 | break; |
| 839 | case Primitive::kPrimVoid: // Intentional fallthrough. |
| 840 | default: |
| 841 | LOG(FATAL) << "Can't determine entrypoint for: " << type; |
| 842 | target = kQuickGet32Instance; |
| 843 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 844 | // Second argument of pGetXXInstance is always a reference. |
| 845 | DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U); |
| 846 | CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true); |
| 847 | |
Serguei Katkov | 4eca9f5 | 2014-07-08 00:45:45 +0700 | [diff] [blame] | 848 | // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp. |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 849 | if (IsWide(size)) { |
Serguei Katkov | 4eca9f5 | 2014-07-08 00:45:45 +0700 | [diff] [blame] | 850 | RegLocation rl_result = GetReturnWide(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 851 | StoreValueWide(rl_dest, rl_result); |
| 852 | } else { |
Serguei Katkov | 4eca9f5 | 2014-07-08 00:45:45 +0700 | [diff] [blame] | 853 | RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 854 | StoreValue(rl_dest, rl_result); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 859 | void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 860 | RegLocation rl_src, RegLocation rl_obj) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 861 | const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir); |
| 862 | cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut()); |
Douglas Leung | d9cb8ae | 2014-07-09 14:28:35 -0700 | [diff] [blame] | 863 | if (!SLOW_FIELD_PATH && field_info.FastPut()) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 864 | RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile()); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 865 | // Dex code never writes to the class field. |
| 866 | DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()), |
| 867 | sizeof(mirror::HeapReference<mirror::Class>)); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 868 | rl_obj = LoadValue(rl_obj, kRefReg); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 869 | if (IsWide(size)) { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 870 | rl_src = LoadValueWide(rl_src, reg_class); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 871 | } else { |
| 872 | rl_src = LoadValue(rl_src, reg_class); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 873 | } |
| 874 | GenNullCheck(rl_obj.reg, opt_flags); |
| 875 | int field_offset = field_info.FieldOffset().Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 876 | LIR* store; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 877 | if (IsRef(size)) { |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 878 | store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ? |
| 879 | kVolatile : kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 880 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 881 | store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size, |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 882 | field_info.IsVolatile() ? kVolatile : kNotVolatile); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 883 | } |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 884 | MarkPossibleNullPointerExceptionAfter(opt_flags, store); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 885 | if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 886 | MarkGCCard(rl_src.reg, rl_obj.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 887 | } |
| 888 | } else { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 889 | QuickEntrypointEnum target; |
| 890 | switch (size) { |
| 891 | case kReference: |
| 892 | target = kQuickSetObjInstance; |
| 893 | break; |
| 894 | case k64: |
| 895 | case kDouble: |
| 896 | target = kQuickSet64Instance; |
| 897 | break; |
| 898 | case k32: |
| 899 | case kSingle: |
| 900 | target = kQuickSet32Instance; |
| 901 | break; |
| 902 | case kSignedHalf: |
| 903 | case kUnsignedHalf: |
| 904 | target = kQuickSet16Instance; |
| 905 | break; |
| 906 | case kSignedByte: |
| 907 | case kUnsignedByte: |
| 908 | target = kQuickSet8Instance; |
| 909 | break; |
| 910 | case kWord: // Intentional fallthrough. |
| 911 | default: |
| 912 | LOG(FATAL) << "Can't determine entrypoint for: " << size; |
| 913 | target = kQuickSet32Instance; |
| 914 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 915 | CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src, |
| 916 | true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 920 | void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index, |
| 921 | RegLocation rl_src) { |
| 922 | bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK); |
| 923 | bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) && |
| 924 | (opt_flags & MIR_IGNORE_NULL_CHECK)); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 925 | QuickEntrypointEnum target = needs_range_check |
| 926 | ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck |
| 927 | : kQuickAputObjectWithBoundCheck) |
| 928 | : kQuickAputObject; |
| 929 | CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 932 | void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 933 | RegLocation rl_method = LoadCurrMethod(); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 934 | CheckRegLocation(rl_method); |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 935 | RegStorage res_reg = AllocTempRef(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 936 | RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 937 | if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 938 | *cu_->dex_file, |
| 939 | type_idx)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 940 | // Call out to helper which resolves type and verifies access. |
| 941 | // Resolved type returned in kRet0. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 942 | CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 943 | RegLocation rl_result = GetReturn(kRefReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 944 | StoreValue(rl_dest, rl_result); |
| 945 | } else { |
| 946 | // We're don't need access checks, load type from dex cache |
| 947 | int32_t dex_cache_offset = |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 948 | mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 949 | LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 950 | int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 951 | LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 952 | if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, |
| 953 | type_idx) || SLOW_TYPE_PATH) { |
| 954 | // Slow path, at runtime test if type is null and if so initialize |
| 955 | FlushAllRegs(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 956 | LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 957 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
| 958 | |
| 959 | // Object to generate the slow path for class resolution. |
| 960 | class SlowPath : public LIRSlowPath { |
| 961 | public: |
| 962 | SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx, |
| 963 | const RegLocation& rl_method, const RegLocation& rl_result) : |
| 964 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx), |
| 965 | rl_method_(rl_method), rl_result_(rl_result) { |
| 966 | } |
| 967 | |
| 968 | void Compile() { |
| 969 | GenerateTargetLabel(); |
| 970 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 971 | m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 972 | m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef)); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 973 | m2l_->OpUnconditionalBranch(cont_); |
| 974 | } |
| 975 | |
| 976 | private: |
| 977 | const int type_idx_; |
| 978 | const RegLocation rl_method_; |
| 979 | const RegLocation rl_result_; |
| 980 | }; |
| 981 | |
| 982 | // Add to list for future. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 983 | AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result)); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 984 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 985 | StoreValue(rl_dest, rl_result); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 986 | } else { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 987 | // Fast path, we're done - just store result |
| 988 | StoreValue(rl_dest, rl_result); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 993 | void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 994 | /* NOTE: Most strings should be available at compile time */ |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 995 | int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx). |
| 996 | Int32Value(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 997 | if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache( |
| 998 | *cu_->dex_file, string_idx) || SLOW_STRING_PATH) { |
| 999 | // slow path, resolve string if not in dex cache |
| 1000 | FlushAllRegs(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1001 | LockCallTemps(); // Using explicit registers |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1002 | |
| 1003 | // If the Method* is already in a register, we can save a copy. |
| 1004 | RegLocation rl_method = mir_graph_->GetMethodLoc(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1005 | RegStorage r_method; |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1006 | if (rl_method.location == kLocPhysReg) { |
| 1007 | // A temp would conflict with register use below. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1008 | DCHECK(!IsTemp(rl_method.reg)); |
| 1009 | r_method = rl_method.reg; |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1010 | } else { |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1011 | r_method = TargetReg(kArg2, kRef); |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1012 | LoadCurrMethodDirect(r_method); |
| 1013 | } |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 1014 | LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1015 | TargetReg(kArg0, kRef), kNotVolatile); |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1016 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1017 | // Might call out to helper, which will return resolved string in kRet0 |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1018 | LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile); |
| 1019 | LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL); |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1020 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
Mark Mendell | 766e929 | 2014-01-27 07:55:47 -0800 | [diff] [blame] | 1021 | |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1022 | { |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1023 | // Object to generate the slow path for string resolution. |
| 1024 | class SlowPath : public LIRSlowPath { |
| 1025 | public: |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1026 | SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) : |
| 1027 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), |
| 1028 | r_method_(r_method), string_idx_(string_idx) { |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | void Compile() { |
| 1032 | GenerateTargetLabel(); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1033 | m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1034 | m2l_->OpUnconditionalBranch(cont_); |
| 1035 | } |
| 1036 | |
| 1037 | private: |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1038 | const RegStorage r_method_; |
| 1039 | const int32_t string_idx_; |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1040 | }; |
| 1041 | |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1042 | AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1043 | } |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1044 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1045 | GenBarrier(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1046 | StoreValue(rl_dest, GetReturn(kRefReg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1047 | } else { |
| 1048 | RegLocation rl_method = LoadCurrMethod(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1049 | RegStorage res_reg = AllocTempRef(); |
| 1050 | RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1051 | LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg, |
| 1052 | kNotVolatile); |
| 1053 | LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1054 | StoreValue(rl_dest, rl_result); |
| 1055 | } |
| 1056 | } |
| 1057 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1058 | /* |
| 1059 | * Let helper function take care of everything. Will |
| 1060 | * call Class::NewInstanceFromCode(type_idx, method); |
| 1061 | */ |
| 1062 | void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) { |
| 1063 | FlushAllRegs(); /* Everything to home location */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1064 | // alloc will always check for resolution, do we also need to verify |
| 1065 | // access because the verifier was unable to? |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1066 | const DexFile* dex_file = cu_->dex_file; |
| 1067 | CompilerDriver* driver = cu_->compiler_driver; |
| 1068 | if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) { |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1069 | bool is_type_initialized; |
| 1070 | bool use_direct_type_ptr; |
| 1071 | uintptr_t direct_type_ptr; |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 1072 | bool is_finalizable; |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1073 | if (kEmbedClassInCode && |
Mathieu Chartier | 8668c3c | 2014-04-24 16:48:11 -0700 | [diff] [blame] | 1074 | driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr, |
| 1075 | &direct_type_ptr, &is_finalizable) && |
| 1076 | !is_finalizable) { |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1077 | // The fast path. |
| 1078 | if (!use_direct_type_ptr) { |
Fred Shih | e7f82e2 | 2014-08-06 10:46:37 -0700 | [diff] [blame] | 1079 | LoadClassType(*dex_file, type_idx, kArg0); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1080 | if (!is_type_initialized) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1081 | CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1082 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1083 | CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } else { |
| 1086 | // Use the direct pointer. |
| 1087 | if (!is_type_initialized) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1088 | CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1089 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1090 | CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | } else { |
| 1094 | // The slow path. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1095 | CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1096 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1097 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1098 | CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1099 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1100 | StoreValue(rl_dest, GetReturn(kRefReg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1103 | void Mir2Lir::GenThrow(RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1104 | FlushAllRegs(); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1105 | CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | // For final classes there are no sub-classes to check and so we can answer the instance-of |
| 1109 | // question with simple comparisons. |
| 1110 | void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest, |
| 1111 | RegLocation rl_src) { |
Mark Mendell | df8ee2e | 2014-01-27 16:37:47 -0800 | [diff] [blame] | 1112 | // X86 has its own implementation. |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 1113 | DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64); |
Mark Mendell | df8ee2e | 2014-01-27 16:37:47 -0800 | [diff] [blame] | 1114 | |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1115 | RegLocation object = LoadValue(rl_src, kRefReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1116 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1117 | RegStorage result_reg = rl_result.reg; |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame] | 1118 | if (IsSameReg(result_reg, object.reg)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1119 | result_reg = AllocTypedTemp(false, kCoreReg); |
buzbee | b5860fb | 2014-06-21 15:31:01 -0700 | [diff] [blame] | 1120 | DCHECK(!IsSameReg(result_reg, object.reg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1121 | } |
| 1122 | LoadConstant(result_reg, 0); // assume false |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1123 | LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1124 | |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1125 | RegStorage check_class = AllocTypedTemp(false, kRefReg); |
| 1126 | RegStorage object_class = AllocTypedTemp(false, kRefReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1127 | |
| 1128 | LoadCurrMethodDirect(check_class); |
| 1129 | if (use_declaring_class) { |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1130 | LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class, |
| 1131 | kNotVolatile); |
| 1132 | LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class, |
| 1133 | kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1134 | } else { |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 1135 | LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1136 | check_class, kNotVolatile); |
| 1137 | LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class, |
| 1138 | kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 1139 | int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1140 | LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 1143 | // FIXME: what should we be comparing here? compressed or decompressed references? |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1144 | if (cu_->instruction_set == kThumb2) { |
| 1145 | OpRegReg(kOpCmp, check_class, object_class); // Same? |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 1146 | LIR* it = OpIT(kCondEq, ""); // if-convert the test |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1147 | LoadConstant(result_reg, 1); // .eq case - load true |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 1148 | OpEndIT(it); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1149 | } else { |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1150 | GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1151 | } |
| 1152 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 1153 | null_branchover->target = target; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1154 | FreeTemp(object_class); |
| 1155 | FreeTemp(check_class); |
| 1156 | if (IsTemp(result_reg)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1157 | OpRegCopy(rl_result.reg, result_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1158 | FreeTemp(result_reg); |
| 1159 | } |
| 1160 | StoreValue(rl_dest, rl_result); |
| 1161 | } |
| 1162 | |
| 1163 | void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final, |
| 1164 | bool type_known_abstract, bool use_declaring_class, |
| 1165 | bool can_assume_type_is_in_dex_cache, |
| 1166 | uint32_t type_idx, RegLocation rl_dest, |
| 1167 | RegLocation rl_src) { |
| 1168 | FlushAllRegs(); |
| 1169 | // May generate a call - use explicit registers |
| 1170 | LockCallTemps(); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1171 | RegStorage method_reg = TargetReg(kArg1, kRef); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1172 | LoadCurrMethodDirect(method_reg); // kArg1 <= current Method* |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1173 | RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class* |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1174 | RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref. |
| 1175 | RegStorage ret_reg = GetReturn(kRefReg).reg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1176 | if (needs_access_check) { |
| 1177 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 1178 | // returns Class* in kArg0 |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1179 | CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true); |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1180 | OpRegCopy(class_reg, ret_reg); // Align usage with fast path |
| 1181 | LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1182 | } else if (use_declaring_class) { |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1183 | LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1184 | LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1185 | class_reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1186 | } else { |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1187 | if (can_assume_type_is_in_dex_cache) { |
| 1188 | // Conditionally, as in the other case we will also load it. |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1189 | LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1192 | // Load dex cache entry into class_reg (kArg2) |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1193 | LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1194 | class_reg, kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 1195 | int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1196 | LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1197 | if (!can_assume_type_is_in_dex_cache) { |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1198 | LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL); |
| 1199 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 1200 | |
| 1201 | // Should load value here. |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1202 | LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1203 | |
| 1204 | class InitTypeSlowPath : public Mir2Lir::LIRSlowPath { |
| 1205 | public: |
| 1206 | InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx, |
| 1207 | RegLocation rl_src) |
| 1208 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx), |
| 1209 | rl_src_(rl_src) { |
| 1210 | } |
| 1211 | |
| 1212 | void Compile() OVERRIDE { |
| 1213 | GenerateTargetLabel(); |
| 1214 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1215 | m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true); |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1216 | m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef), |
| 1217 | m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1218 | m2l_->OpUnconditionalBranch(cont_); |
| 1219 | } |
| 1220 | |
| 1221 | private: |
| 1222 | uint32_t type_idx_; |
| 1223 | RegLocation rl_src_; |
| 1224 | }; |
| 1225 | |
| 1226 | AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target, |
| 1227 | type_idx, rl_src)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */ |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1231 | RegLocation rl_result = GetReturn(kCoreReg); |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1232 | if (!IsSameReg(rl_result.reg, ref_reg)) { |
| 1233 | // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1234 | LoadConstant(rl_result.reg, 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1235 | } |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1236 | LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1237 | |
| 1238 | /* load object->klass_ */ |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1239 | RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1240 | DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0); |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1241 | LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(), |
| 1242 | ref_class_reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1243 | /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */ |
| 1244 | LIR* branchover = NULL; |
| 1245 | if (type_known_final) { |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1246 | // rl_result == ref == class. |
| 1247 | GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg, |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1248 | kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1249 | } else { |
| 1250 | if (cu_->instruction_set == kThumb2) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1251 | RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial); |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 1252 | LIR* it = nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1253 | if (!type_known_abstract) { |
| 1254 | /* Uses conditional nullification */ |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1255 | OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same? |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 1256 | it = OpIT(kCondEq, "EE"); // if-convert the test |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1257 | LoadConstant(rl_result.reg, 1); // .eq case - load true |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1258 | } |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1259 | OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1260 | OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class) |
Dave Allison | 3da67a5 | 2014-04-02 17:03:45 -0700 | [diff] [blame] | 1261 | if (it != nullptr) { |
| 1262 | OpEndIT(it); |
| 1263 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1264 | FreeTemp(r_tgt); |
| 1265 | } else { |
| 1266 | if (!type_known_abstract) { |
| 1267 | /* Uses branchovers */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1268 | LoadConstant(rl_result.reg, 1); // assume true |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1269 | branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1270 | } |
Andreas Gampe | 90969af | 2014-07-15 23:02:11 -0700 | [diff] [blame] | 1271 | |
Serguei Katkov | 9ee4519 | 2014-07-17 14:39:03 +0700 | [diff] [blame] | 1272 | OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1273 | CallRuntimeHelper(kQuickInstanceofNonTrivial, false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | // TODO: only clobber when type isn't final? |
Vladimir Marko | 31c2aac | 2013-12-09 16:31:19 +0000 | [diff] [blame] | 1277 | ClobberCallerSave(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1278 | /* branch targets here */ |
| 1279 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 1280 | StoreValue(rl_dest, rl_result); |
| 1281 | branch1->target = target; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1282 | if (branchover != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1283 | branchover->target = target; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) { |
| 1288 | bool type_known_final, type_known_abstract, use_declaring_class; |
| 1289 | bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, |
| 1290 | *cu_->dex_file, |
| 1291 | type_idx, |
| 1292 | &type_known_final, |
| 1293 | &type_known_abstract, |
| 1294 | &use_declaring_class); |
| 1295 | bool can_assume_type_is_in_dex_cache = !needs_access_check && |
| 1296 | cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx); |
| 1297 | |
| 1298 | if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) { |
| 1299 | GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src); |
| 1300 | } else { |
| 1301 | GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract, |
| 1302 | use_declaring_class, can_assume_type_is_in_dex_cache, |
| 1303 | type_idx, rl_dest, rl_src); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1307 | void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1308 | bool type_known_final, type_known_abstract, use_declaring_class; |
| 1309 | bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, |
| 1310 | *cu_->dex_file, |
| 1311 | type_idx, |
| 1312 | &type_known_final, |
| 1313 | &type_known_abstract, |
| 1314 | &use_declaring_class); |
| 1315 | // Note: currently type_known_final is unused, as optimizing will only improve the performance |
| 1316 | // of the exception throw path. |
| 1317 | DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit(); |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 1318 | if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1319 | // Verifier type analysis proved this check cast would never cause an exception. |
| 1320 | return; |
| 1321 | } |
| 1322 | FlushAllRegs(); |
| 1323 | // May generate a call - use explicit registers |
| 1324 | LockCallTemps(); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1325 | RegStorage method_reg = TargetReg(kArg1, kRef); |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1326 | LoadCurrMethodDirect(method_reg); // kArg1 <= current Method* |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1327 | RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class* |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1328 | if (needs_access_check) { |
| 1329 | // Check we have access to type_idx and if not throw IllegalAccessError, |
| 1330 | // returns Class* in kRet0 |
| 1331 | // InitializeTypeAndVerifyAccess(idx, method) |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1332 | CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1333 | OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1334 | } else if (use_declaring_class) { |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1335 | LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1336 | class_reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1337 | } else { |
| 1338 | // Load dex cache entry into class_reg (kArg2) |
Andreas Gampe | 4b537a8 | 2014-06-30 22:24:53 -0700 | [diff] [blame] | 1339 | LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1340 | class_reg, kNotVolatile); |
Andreas Gampe | 9c3b089 | 2014-04-24 17:33:34 +0000 | [diff] [blame] | 1341 | int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value(); |
Andreas Gampe | 3c12c51 | 2014-06-24 18:46:29 +0000 | [diff] [blame] | 1342 | LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1343 | if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) { |
| 1344 | // Need to test presence of type in dex cache at runtime |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1345 | LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL); |
| 1346 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
| 1347 | |
| 1348 | // Slow path to initialize the type. Executed if the type is NULL. |
| 1349 | class SlowPath : public LIRSlowPath { |
| 1350 | public: |
| 1351 | SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx, |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1352 | const RegStorage class_reg) : |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1353 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx), |
| 1354 | class_reg_(class_reg) { |
| 1355 | } |
| 1356 | |
| 1357 | void Compile() { |
| 1358 | GenerateTargetLabel(); |
| 1359 | |
| 1360 | // Call out to helper, which will return resolved type in kArg0 |
| 1361 | // InitializeTypeFromCode(idx, method) |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1362 | m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, |
| 1363 | m2l_->TargetReg(kArg1, kRef), true); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1364 | m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1365 | m2l_->OpUnconditionalBranch(cont_); |
| 1366 | } |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 1367 | |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1368 | public: |
| 1369 | const int type_idx_; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1370 | const RegStorage class_reg_; |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1371 | }; |
| 1372 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1373 | AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | // At this point, class_reg (kArg2) has class |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1377 | LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1378 | |
| 1379 | // Slow path for the case where the classes are not equal. In this case we need |
| 1380 | // to call a helper function to do the check. |
| 1381 | class SlowPath : public LIRSlowPath { |
| 1382 | public: |
| 1383 | SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load): |
| 1384 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) { |
| 1385 | } |
| 1386 | |
| 1387 | void Compile() { |
| 1388 | GenerateTargetLabel(); |
| 1389 | |
| 1390 | if (load_) { |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1391 | m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(), |
| 1392 | m2l_->TargetReg(kArg1, kRef), kNotVolatile); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1393 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1394 | m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef), |
| 1395 | m2l_->TargetReg(kArg1, kRef), true); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1396 | m2l_->OpUnconditionalBranch(cont_); |
| 1397 | } |
| 1398 | |
| 1399 | private: |
Mingyao Yang | 3b004ba | 2014-04-29 15:55:37 -0700 | [diff] [blame] | 1400 | const bool load_; |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1401 | }; |
| 1402 | |
| 1403 | if (type_known_abstract) { |
| 1404 | // Easier case, run slow path if target is non-null (slow path will load from target) |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1405 | LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1406 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
| 1407 | AddSlowPath(new (arena_) SlowPath(this, branch, cont, true)); |
| 1408 | } else { |
| 1409 | // Harder, more common case. We need to generate a forward branch over the load |
| 1410 | // if the target is null. If it's non-null we perform the load and branch to the |
| 1411 | // slow path if the classes are not equal. |
| 1412 | |
| 1413 | /* Null is OK - continue */ |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1414 | LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1415 | /* load object->klass_ */ |
| 1416 | DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1417 | LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(), |
| 1418 | TargetReg(kArg1, kRef), kNotVolatile); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1419 | |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1420 | LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr); |
Dave Allison | bcec6fb | 2014-01-17 12:52:22 -0800 | [diff] [blame] | 1421 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
| 1422 | |
| 1423 | // Add the slow path that will not perform load since this is already done. |
| 1424 | AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false)); |
| 1425 | |
| 1426 | // Set the null check to branch to the continuation. |
| 1427 | branch1->target = cont; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1432 | RegLocation rl_src1, RegLocation rl_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1433 | RegLocation rl_result; |
| 1434 | if (cu_->instruction_set == kThumb2) { |
| 1435 | /* |
| 1436 | * NOTE: This is the one place in the code in which we might have |
| 1437 | * as many as six live temporary registers. There are 5 in the normal |
| 1438 | * set for Arm. Until we have spill capabilities, temporarily add |
| 1439 | * lr to the temp set. It is safe to do this locally, but note that |
| 1440 | * lr is used explicitly elsewhere in the code generator and cannot |
| 1441 | * normally be used as a general temp register. |
| 1442 | */ |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1443 | MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool |
| 1444 | FreeTemp(TargetReg(kLr, kNotWide)); // and make it available |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1445 | } |
| 1446 | rl_src1 = LoadValueWide(rl_src1, kCoreReg); |
| 1447 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 1448 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1449 | // The longs may overlap - use intermediate temp if so |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1450 | if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) { |
| 1451 | RegStorage t_reg = AllocTemp(); |
| 1452 | OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow()); |
| 1453 | OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh()); |
| 1454 | OpRegCopy(rl_result.reg.GetLow(), t_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1455 | FreeTemp(t_reg); |
| 1456 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1457 | OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow()); |
| 1458 | OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1459 | } |
| 1460 | /* |
| 1461 | * NOTE: If rl_dest refers to a frame variable in a large frame, the |
| 1462 | * following StoreValueWide might need to allocate a temp register. |
| 1463 | * To further work around the lack of a spill capability, explicitly |
| 1464 | * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result. |
| 1465 | * Remove when spill is functional. |
| 1466 | */ |
| 1467 | FreeRegLocTemps(rl_result, rl_src1); |
| 1468 | FreeRegLocTemps(rl_result, rl_src2); |
| 1469 | StoreValueWide(rl_dest, rl_result); |
| 1470 | if (cu_->instruction_set == kThumb2) { |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1471 | Clobber(TargetReg(kLr, kNotWide)); |
| 1472 | UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1476 | void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest, |
| 1477 | RegLocation rl_src1, RegLocation rl_shift) { |
| 1478 | QuickEntrypointEnum target; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1479 | switch (opcode) { |
| 1480 | case Instruction::SHL_LONG: |
| 1481 | case Instruction::SHL_LONG_2ADDR: |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1482 | target = kQuickShlLong; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | case Instruction::SHR_LONG: |
| 1485 | case Instruction::SHR_LONG_2ADDR: |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1486 | target = kQuickShrLong; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1487 | break; |
| 1488 | case Instruction::USHR_LONG: |
| 1489 | case Instruction::USHR_LONG_2ADDR: |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1490 | target = kQuickUshrLong; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1491 | break; |
| 1492 | default: |
| 1493 | LOG(FATAL) << "Unexpected case"; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1494 | target = kQuickShlLong; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1495 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1496 | FlushAllRegs(); /* Send everything to home location */ |
| 1497 | CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1498 | RegLocation rl_result = GetReturnWide(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1499 | StoreValueWide(rl_dest, rl_result); |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest, |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 1504 | RegLocation rl_src1, RegLocation rl_src2, int flags) { |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 1505 | DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1506 | OpKind op = kOpBkpt; |
| 1507 | bool is_div_rem = false; |
| 1508 | bool check_zero = false; |
| 1509 | bool unary = false; |
| 1510 | RegLocation rl_result; |
| 1511 | bool shift_op = false; |
| 1512 | switch (opcode) { |
| 1513 | case Instruction::NEG_INT: |
| 1514 | op = kOpNeg; |
| 1515 | unary = true; |
| 1516 | break; |
| 1517 | case Instruction::NOT_INT: |
| 1518 | op = kOpMvn; |
| 1519 | unary = true; |
| 1520 | break; |
| 1521 | case Instruction::ADD_INT: |
| 1522 | case Instruction::ADD_INT_2ADDR: |
| 1523 | op = kOpAdd; |
| 1524 | break; |
| 1525 | case Instruction::SUB_INT: |
| 1526 | case Instruction::SUB_INT_2ADDR: |
| 1527 | op = kOpSub; |
| 1528 | break; |
| 1529 | case Instruction::MUL_INT: |
| 1530 | case Instruction::MUL_INT_2ADDR: |
| 1531 | op = kOpMul; |
| 1532 | break; |
| 1533 | case Instruction::DIV_INT: |
| 1534 | case Instruction::DIV_INT_2ADDR: |
| 1535 | check_zero = true; |
| 1536 | op = kOpDiv; |
| 1537 | is_div_rem = true; |
| 1538 | break; |
| 1539 | /* NOTE: returns in kArg1 */ |
| 1540 | case Instruction::REM_INT: |
| 1541 | case Instruction::REM_INT_2ADDR: |
| 1542 | check_zero = true; |
| 1543 | op = kOpRem; |
| 1544 | is_div_rem = true; |
| 1545 | break; |
| 1546 | case Instruction::AND_INT: |
| 1547 | case Instruction::AND_INT_2ADDR: |
| 1548 | op = kOpAnd; |
| 1549 | break; |
| 1550 | case Instruction::OR_INT: |
| 1551 | case Instruction::OR_INT_2ADDR: |
| 1552 | op = kOpOr; |
| 1553 | break; |
| 1554 | case Instruction::XOR_INT: |
| 1555 | case Instruction::XOR_INT_2ADDR: |
| 1556 | op = kOpXor; |
| 1557 | break; |
| 1558 | case Instruction::SHL_INT: |
| 1559 | case Instruction::SHL_INT_2ADDR: |
| 1560 | shift_op = true; |
| 1561 | op = kOpLsl; |
| 1562 | break; |
| 1563 | case Instruction::SHR_INT: |
| 1564 | case Instruction::SHR_INT_2ADDR: |
| 1565 | shift_op = true; |
| 1566 | op = kOpAsr; |
| 1567 | break; |
| 1568 | case Instruction::USHR_INT: |
| 1569 | case Instruction::USHR_INT_2ADDR: |
| 1570 | shift_op = true; |
| 1571 | op = kOpLsr; |
| 1572 | break; |
| 1573 | default: |
| 1574 | LOG(FATAL) << "Invalid word arith op: " << opcode; |
| 1575 | } |
| 1576 | if (!is_div_rem) { |
| 1577 | if (unary) { |
| 1578 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 1579 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1580 | OpRegReg(op, rl_result.reg, rl_src1.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1581 | } else { |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 1582 | if ((shift_op) && (cu_->instruction_set != kArm64)) { |
Mark Mendell | feb2b4e | 2014-01-28 12:59:49 -0800 | [diff] [blame] | 1583 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1584 | RegStorage t_reg = AllocTemp(); |
| 1585 | OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1586 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 1587 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1588 | OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1589 | FreeTemp(t_reg); |
| 1590 | } else { |
| 1591 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 1592 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
| 1593 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1594 | OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | StoreValue(rl_dest, rl_result); |
| 1598 | } else { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1599 | bool done = false; // Set to true if we happen to find a way to use a real instruction. |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 1600 | if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1601 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 1602 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 1603 | if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) { |
Mingyao Yang | d15f4e2 | 2014-04-17 18:46:24 -0700 | [diff] [blame] | 1604 | GenDivZeroCheck(rl_src2.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1605 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1606 | rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1607 | done = true; |
| 1608 | } else if (cu_->instruction_set == kThumb2) { |
| 1609 | if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) { |
| 1610 | // Use ARM SDIV instruction for division. For remainder we also need to |
| 1611 | // calculate using a MUL and subtract. |
| 1612 | rl_src1 = LoadValue(rl_src1, kCoreReg); |
| 1613 | rl_src2 = LoadValue(rl_src2, kCoreReg); |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 1614 | if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) { |
Mingyao Yang | d15f4e2 | 2014-04-17 18:46:24 -0700 | [diff] [blame] | 1615 | GenDivZeroCheck(rl_src2.reg); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1616 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1617 | rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1618 | done = true; |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | // If we haven't already generated the code use the callout function. |
| 1623 | if (!done) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1624 | FlushAllRegs(); /* Send everything to home location */ |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1625 | LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide)); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1626 | RegStorage r_tgt = CallHelperSetup(kQuickIdivmod); |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1627 | LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide)); |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 1628 | if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) { |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1629 | GenDivZeroCheck(TargetReg(kArg1, kNotWide)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1630 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1631 | // NOTE: callout here is not a safepoint. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1632 | CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1633 | if (op == kOpDiv) |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1634 | rl_result = GetReturn(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1635 | else |
| 1636 | rl_result = GetReturnAlt(); |
| 1637 | } |
| 1638 | StoreValue(rl_dest, rl_result); |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | /* |
| 1643 | * The following are the first-level codegen routines that analyze the format |
| 1644 | * of each bytecode then either dispatch special purpose codegen routines |
| 1645 | * or produce corresponding Thumb instructions directly. |
| 1646 | */ |
| 1647 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1648 | // Returns true if no more than two bits are set in 'x'. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1649 | static bool IsPopCountLE2(unsigned int x) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1650 | x &= x - 1; |
| 1651 | return (x & (x - 1)) == 0; |
| 1652 | } |
| 1653 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1654 | // Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit' |
| 1655 | // and store the result in 'rl_dest'. |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1656 | bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1657 | RegLocation rl_src, RegLocation rl_dest, int lit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1658 | if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) { |
| 1659 | return false; |
| 1660 | } |
| 1661 | // No divide instruction for Arm, so check for more special cases |
| 1662 | if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) { |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1663 | return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1664 | } |
| 1665 | int k = LowestSetBit(lit); |
| 1666 | if (k >= 30) { |
| 1667 | // Avoid special cases. |
| 1668 | return false; |
| 1669 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1670 | rl_src = LoadValue(rl_src, kCoreReg); |
| 1671 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1672 | if (is_div) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1673 | RegStorage t_reg = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1674 | if (lit == 2) { |
| 1675 | // Division by 2 is by far the most common division by constant. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1676 | OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k); |
| 1677 | OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg); |
| 1678 | OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1679 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1680 | OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1681 | OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1682 | OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg); |
| 1683 | OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1686 | RegStorage t_reg1 = AllocTemp(); |
| 1687 | RegStorage t_reg2 = AllocTemp(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1688 | if (lit == 2) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1689 | OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k); |
| 1690 | OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1691 | OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1692 | OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1693 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1694 | OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1695 | OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1696 | OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1697 | OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1698 | OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | StoreValue(rl_dest, rl_result); |
| 1702 | return true; |
| 1703 | } |
| 1704 | |
| 1705 | // Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit' |
| 1706 | // and store the result in 'rl_dest'. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1707 | bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) { |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1708 | if (lit < 0) { |
| 1709 | return false; |
| 1710 | } |
| 1711 | if (lit == 0) { |
| 1712 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1713 | LoadConstant(rl_result.reg, 0); |
| 1714 | StoreValue(rl_dest, rl_result); |
| 1715 | return true; |
| 1716 | } |
| 1717 | if (lit == 1) { |
| 1718 | rl_src = LoadValue(rl_src, kCoreReg); |
| 1719 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1720 | OpRegCopy(rl_result.reg, rl_src.reg); |
| 1721 | StoreValue(rl_dest, rl_result); |
| 1722 | return true; |
| 1723 | } |
Zheng Xu | f9719f9 | 2014-04-02 13:31:31 +0100 | [diff] [blame] | 1724 | // There is RegRegRegShift on Arm, so check for more special cases |
| 1725 | if (cu_->instruction_set == kThumb2) { |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1726 | return EasyMultiply(rl_src, rl_dest, lit); |
| 1727 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1728 | // Can we simplify this multiplication? |
| 1729 | bool power_of_two = false; |
| 1730 | bool pop_count_le2 = false; |
| 1731 | bool power_of_two_minus_one = false; |
Ian Rogers | e2143c0 | 2014-03-28 08:47:16 -0700 | [diff] [blame] | 1732 | if (IsPowerOfTwo(lit)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1733 | power_of_two = true; |
| 1734 | } else if (IsPopCountLE2(lit)) { |
| 1735 | pop_count_le2 = true; |
| 1736 | } else if (IsPowerOfTwo(lit + 1)) { |
| 1737 | power_of_two_minus_one = true; |
| 1738 | } else { |
| 1739 | return false; |
| 1740 | } |
| 1741 | rl_src = LoadValue(rl_src, kCoreReg); |
| 1742 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1743 | if (power_of_two) { |
| 1744 | // Shift. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1745 | OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1746 | } else if (pop_count_le2) { |
| 1747 | // Shift and add and shift. |
| 1748 | int first_bit = LowestSetBit(lit); |
| 1749 | int second_bit = LowestSetBit(lit ^ (1 << first_bit)); |
| 1750 | GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit); |
| 1751 | } else { |
| 1752 | // Reverse subtract: (src << (shift + 1)) - src. |
| 1753 | DCHECK(power_of_two_minus_one); |
| 1754 | // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1) |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1755 | RegStorage t_reg = AllocTemp(); |
| 1756 | OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1)); |
| 1757 | OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1758 | } |
| 1759 | StoreValue(rl_dest, rl_result); |
| 1760 | return true; |
| 1761 | } |
| 1762 | |
| 1763 | void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1764 | int lit) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1765 | RegLocation rl_result; |
| 1766 | OpKind op = static_cast<OpKind>(0); /* Make gcc happy */ |
| 1767 | int shift_op = false; |
| 1768 | bool is_div = false; |
| 1769 | |
| 1770 | switch (opcode) { |
| 1771 | case Instruction::RSUB_INT_LIT8: |
| 1772 | case Instruction::RSUB_INT: { |
| 1773 | rl_src = LoadValue(rl_src, kCoreReg); |
| 1774 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 1775 | if (cu_->instruction_set == kThumb2) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1776 | OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1777 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1778 | OpRegReg(kOpNeg, rl_result.reg, rl_src.reg); |
| 1779 | OpRegImm(kOpAdd, rl_result.reg, lit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1780 | } |
| 1781 | StoreValue(rl_dest, rl_result); |
| 1782 | return; |
| 1783 | } |
| 1784 | |
| 1785 | case Instruction::SUB_INT: |
| 1786 | case Instruction::SUB_INT_2ADDR: |
| 1787 | lit = -lit; |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 1788 | FALLTHROUGH_INTENDED; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1789 | case Instruction::ADD_INT: |
| 1790 | case Instruction::ADD_INT_2ADDR: |
| 1791 | case Instruction::ADD_INT_LIT8: |
| 1792 | case Instruction::ADD_INT_LIT16: |
| 1793 | op = kOpAdd; |
| 1794 | break; |
| 1795 | case Instruction::MUL_INT: |
| 1796 | case Instruction::MUL_INT_2ADDR: |
| 1797 | case Instruction::MUL_INT_LIT8: |
| 1798 | case Instruction::MUL_INT_LIT16: { |
| 1799 | if (HandleEasyMultiply(rl_src, rl_dest, lit)) { |
| 1800 | return; |
| 1801 | } |
| 1802 | op = kOpMul; |
| 1803 | break; |
| 1804 | } |
| 1805 | case Instruction::AND_INT: |
| 1806 | case Instruction::AND_INT_2ADDR: |
| 1807 | case Instruction::AND_INT_LIT8: |
| 1808 | case Instruction::AND_INT_LIT16: |
| 1809 | op = kOpAnd; |
| 1810 | break; |
| 1811 | case Instruction::OR_INT: |
| 1812 | case Instruction::OR_INT_2ADDR: |
| 1813 | case Instruction::OR_INT_LIT8: |
| 1814 | case Instruction::OR_INT_LIT16: |
| 1815 | op = kOpOr; |
| 1816 | break; |
| 1817 | case Instruction::XOR_INT: |
| 1818 | case Instruction::XOR_INT_2ADDR: |
| 1819 | case Instruction::XOR_INT_LIT8: |
| 1820 | case Instruction::XOR_INT_LIT16: |
| 1821 | op = kOpXor; |
| 1822 | break; |
| 1823 | case Instruction::SHL_INT_LIT8: |
| 1824 | case Instruction::SHL_INT: |
| 1825 | case Instruction::SHL_INT_2ADDR: |
| 1826 | lit &= 31; |
| 1827 | shift_op = true; |
| 1828 | op = kOpLsl; |
| 1829 | break; |
| 1830 | case Instruction::SHR_INT_LIT8: |
| 1831 | case Instruction::SHR_INT: |
| 1832 | case Instruction::SHR_INT_2ADDR: |
| 1833 | lit &= 31; |
| 1834 | shift_op = true; |
| 1835 | op = kOpAsr; |
| 1836 | break; |
| 1837 | case Instruction::USHR_INT_LIT8: |
| 1838 | case Instruction::USHR_INT: |
| 1839 | case Instruction::USHR_INT_2ADDR: |
| 1840 | lit &= 31; |
| 1841 | shift_op = true; |
| 1842 | op = kOpLsr; |
| 1843 | break; |
| 1844 | |
| 1845 | case Instruction::DIV_INT: |
| 1846 | case Instruction::DIV_INT_2ADDR: |
| 1847 | case Instruction::DIV_INT_LIT8: |
| 1848 | case Instruction::DIV_INT_LIT16: |
| 1849 | case Instruction::REM_INT: |
| 1850 | case Instruction::REM_INT_2ADDR: |
| 1851 | case Instruction::REM_INT_LIT8: |
| 1852 | case Instruction::REM_INT_LIT16: { |
| 1853 | if (lit == 0) { |
Mingyao Yang | e643a17 | 2014-04-08 11:02:52 -0700 | [diff] [blame] | 1854 | GenDivZeroException(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1855 | return; |
| 1856 | } |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1857 | if ((opcode == Instruction::DIV_INT) || |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1858 | (opcode == Instruction::DIV_INT_2ADDR) || |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1859 | (opcode == Instruction::DIV_INT_LIT8) || |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1860 | (opcode == Instruction::DIV_INT_LIT16)) { |
| 1861 | is_div = true; |
| 1862 | } else { |
| 1863 | is_div = false; |
| 1864 | } |
buzbee | 11b63d1 | 2013-08-27 07:34:17 -0700 | [diff] [blame] | 1865 | if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) { |
| 1866 | return; |
| 1867 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1868 | |
| 1869 | bool done = false; |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 1870 | if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1871 | rl_src = LoadValue(rl_src, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1872 | rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1873 | done = true; |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 1874 | } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) { |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 1875 | rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div); |
| 1876 | done = true; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1877 | } else if (cu_->instruction_set == kThumb2) { |
| 1878 | if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) { |
| 1879 | // Use ARM SDIV instruction for division. For remainder we also need to |
| 1880 | // calculate using a MUL and subtract. |
| 1881 | rl_src = LoadValue(rl_src, kCoreReg); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1882 | rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1883 | done = true; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | if (!done) { |
| 1888 | FlushAllRegs(); /* Everything to home location. */ |
Andreas Gampe | ccc6026 | 2014-07-04 18:02:38 -0700 | [diff] [blame] | 1889 | LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide)); |
| 1890 | Clobber(TargetReg(kArg0, kNotWide)); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1891 | CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1892 | if (is_div) |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 1893 | rl_result = GetReturn(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1894 | else |
| 1895 | rl_result = GetReturnAlt(); |
| 1896 | } |
| 1897 | StoreValue(rl_dest, rl_result); |
| 1898 | return; |
| 1899 | } |
| 1900 | default: |
| 1901 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 1902 | } |
| 1903 | rl_src = LoadValue(rl_src, kCoreReg); |
| 1904 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 1905 | // Avoid shifts by literal 0 - no support in Thumb. Change to copy. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1906 | if (shift_op && (lit == 0)) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1907 | OpRegCopy(rl_result.reg, rl_src.reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1908 | } else { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1909 | OpRegRegImm(op, rl_result.reg, rl_src.reg, lit); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1910 | } |
| 1911 | StoreValue(rl_dest, rl_result); |
| 1912 | } |
| 1913 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1914 | void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 1915 | RegLocation rl_src1, RegLocation rl_src2, int flags) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1916 | RegLocation rl_result; |
| 1917 | OpKind first_op = kOpBkpt; |
| 1918 | OpKind second_op = kOpBkpt; |
| 1919 | bool call_out = false; |
| 1920 | bool check_zero = false; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1921 | int ret_reg = TargetReg(kRet0, kNotWide).GetReg(); |
| 1922 | QuickEntrypointEnum target; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1923 | |
| 1924 | switch (opcode) { |
| 1925 | case Instruction::NOT_LONG: |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1926 | rl_src2 = LoadValueWide(rl_src2, kCoreReg); |
| 1927 | rl_result = EvalLoc(rl_dest, kCoreReg, true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1928 | // Check for destructive overlap |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 1929 | if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1930 | RegStorage t_reg = AllocTemp(); |
| 1931 | OpRegCopy(t_reg, rl_src2.reg.GetHigh()); |
| 1932 | OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow()); |
| 1933 | OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg); |
| 1934 | FreeTemp(t_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1935 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1936 | OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow()); |
| 1937 | OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1938 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1939 | StoreValueWide(rl_dest, rl_result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1940 | return; |
| 1941 | case Instruction::ADD_LONG: |
| 1942 | case Instruction::ADD_LONG_2ADDR: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1943 | first_op = kOpAdd; |
| 1944 | second_op = kOpAdc; |
| 1945 | break; |
| 1946 | case Instruction::SUB_LONG: |
| 1947 | case Instruction::SUB_LONG_2ADDR: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1948 | first_op = kOpSub; |
| 1949 | second_op = kOpSbc; |
| 1950 | break; |
| 1951 | case Instruction::MUL_LONG: |
| 1952 | case Instruction::MUL_LONG_2ADDR: |
Andreas Gampe | c76c614 | 2014-08-04 16:30:03 -0700 | [diff] [blame] | 1953 | call_out = true; |
| 1954 | ret_reg = TargetReg(kRet0, kNotWide).GetReg(); |
| 1955 | target = kQuickLmul; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1956 | break; |
| 1957 | case Instruction::DIV_LONG: |
| 1958 | case Instruction::DIV_LONG_2ADDR: |
| 1959 | call_out = true; |
| 1960 | check_zero = true; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1961 | ret_reg = TargetReg(kRet0, kNotWide).GetReg(); |
| 1962 | target = kQuickLdiv; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1963 | break; |
| 1964 | case Instruction::REM_LONG: |
| 1965 | case Instruction::REM_LONG_2ADDR: |
| 1966 | call_out = true; |
| 1967 | check_zero = true; |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1968 | target = kQuickLmod; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1969 | /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */ |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1970 | ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() : |
| 1971 | TargetReg(kRet0, kNotWide).GetReg(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1972 | break; |
| 1973 | case Instruction::AND_LONG_2ADDR: |
| 1974 | case Instruction::AND_LONG: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1975 | first_op = kOpAnd; |
| 1976 | second_op = kOpAnd; |
| 1977 | break; |
| 1978 | case Instruction::OR_LONG: |
| 1979 | case Instruction::OR_LONG_2ADDR: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1980 | first_op = kOpOr; |
| 1981 | second_op = kOpOr; |
| 1982 | break; |
| 1983 | case Instruction::XOR_LONG: |
| 1984 | case Instruction::XOR_LONG_2ADDR: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1985 | first_op = kOpXor; |
| 1986 | second_op = kOpXor; |
| 1987 | break; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1988 | default: |
| 1989 | LOG(FATAL) << "Invalid long arith op"; |
| 1990 | } |
| 1991 | if (!call_out) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1992 | GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1993 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1994 | FlushAllRegs(); /* Send everything to home location */ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1995 | if (check_zero) { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 1996 | RegStorage r_tmp1 = TargetReg(kArg0, kWide); |
| 1997 | RegStorage r_tmp2 = TargetReg(kArg2, kWide); |
| 1998 | LoadValueDirectWideFixed(rl_src2, r_tmp2); |
| 1999 | RegStorage r_tgt = CallHelperSetup(target); |
Razvan A Lupusoru | 5c5676b | 2014-09-29 16:42:11 -0700 | [diff] [blame^] | 2000 | if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) { |
| 2001 | GenDivZeroCheckWide(r_tmp2); |
| 2002 | } |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2003 | LoadValueDirectWideFixed(rl_src1, r_tmp1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2004 | // NOTE: callout here is not a safepoint |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2005 | CallHelper(r_tgt, target, false /* not safepoint */); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2006 | } else { |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2007 | CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2008 | } |
| 2009 | // Adjust return regs in to handle case of rem returning kArg2/kArg3 |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2010 | if (ret_reg == TargetReg(kRet0, kNotWide).GetReg()) |
| 2011 | rl_result = GetReturnWide(kCoreReg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2012 | else |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2013 | rl_result = GetReturnWideAlt(); |
| 2014 | StoreValueWide(rl_dest, rl_result); |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | |
Mark Mendell | e87f9b5 | 2014-04-30 14:13:18 -0400 | [diff] [blame] | 2018 | void Mir2Lir::GenConst(RegLocation rl_dest, int value) { |
| 2019 | RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true); |
| 2020 | LoadConstantNoClobber(rl_result.reg, value); |
| 2021 | StoreValue(rl_dest, rl_result); |
| 2022 | if (value == 0) { |
| 2023 | Workaround7250540(rl_dest, rl_result.reg); |
| 2024 | } |
| 2025 | } |
| 2026 | |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2027 | void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest, |
| 2028 | RegLocation rl_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2029 | /* |
| 2030 | * Don't optimize the register usage since it calls out to support |
| 2031 | * functions |
| 2032 | */ |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 2033 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2034 | FlushAllRegs(); /* Send everything to home location */ |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2035 | CallRuntimeHelperRegLocation(trampoline, rl_src, false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2036 | if (rl_dest.wide) { |
| 2037 | RegLocation rl_result; |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 2038 | rl_result = GetReturnWide(LocToRegClass(rl_dest)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2039 | StoreValueWide(rl_dest, rl_result); |
| 2040 | } else { |
| 2041 | RegLocation rl_result; |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 2042 | rl_result = GetReturn(LocToRegClass(rl_dest)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2043 | StoreValue(rl_dest, rl_result); |
| 2044 | } |
| 2045 | } |
| 2046 | |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 2047 | class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath { |
| 2048 | public: |
| 2049 | SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont) |
| 2050 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) { |
| 2051 | } |
| 2052 | |
| 2053 | void Compile() OVERRIDE { |
| 2054 | m2l_->ResetRegPool(); |
| 2055 | m2l_->ResetDefTracking(); |
| 2056 | GenerateTargetLabel(kPseudoSuspendTarget); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2057 | m2l_->CallRuntimeHelper(kQuickTestSuspend, true); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 2058 | if (cont_ != nullptr) { |
| 2059 | m2l_->OpUnconditionalBranch(cont_); |
| 2060 | } |
| 2061 | } |
| 2062 | }; |
| 2063 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2064 | /* Check if we need to check for pending suspend request */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 2065 | void Mir2Lir::GenSuspendTest(int opt_flags) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 2066 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 2067 | if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) { |
| 2068 | return; |
| 2069 | } |
| 2070 | FlushAllRegs(); |
| 2071 | LIR* branch = OpTestSuspend(NULL); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 2072 | LIR* cont = NewLIR0(kPseudoTargetLabel); |
| 2073 | AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont)); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 2074 | } else { |
| 2075 | if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) { |
| 2076 | return; |
| 2077 | } |
| 2078 | FlushAllRegs(); // TODO: needed? |
| 2079 | LIR* inst = CheckSuspendUsingLoad(); |
| 2080 | MarkSafepointPC(inst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2081 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* Check if we need to check for pending suspend request */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 2085 | void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 2086 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 2087 | if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) { |
| 2088 | OpUnconditionalBranch(target); |
| 2089 | return; |
| 2090 | } |
| 2091 | OpTestSuspend(target); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 2092 | FlushAllRegs(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 2093 | LIR* branch = OpUnconditionalBranch(nullptr); |
| 2094 | AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target)); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 2095 | } else { |
| 2096 | // For the implicit suspend check, just perform the trigger |
| 2097 | // load and branch to the target. |
| 2098 | if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) { |
| 2099 | OpUnconditionalBranch(target); |
| 2100 | return; |
| 2101 | } |
| 2102 | FlushAllRegs(); |
| 2103 | LIR* inst = CheckSuspendUsingLoad(); |
| 2104 | MarkSafepointPC(inst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2105 | OpUnconditionalBranch(target); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2106 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 2109 | /* Call out to helper assembly routine that will null check obj and then lock it. */ |
| 2110 | void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) { |
| 2111 | FlushAllRegs(); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2112 | CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /* Call out to helper assembly routine that will null check obj and then unlock it. */ |
| 2116 | void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) { |
| 2117 | FlushAllRegs(); |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 2118 | CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
Bill Buzbee | d61ba4b | 2014-01-13 21:44:01 +0000 | [diff] [blame] | 2121 | /* Generic code for generating a wide constant into a VR. */ |
| 2122 | void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) { |
| 2123 | RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 2124 | LoadConstantWide(rl_result.reg, value); |
Bill Buzbee | d61ba4b | 2014-01-13 21:44:01 +0000 | [diff] [blame] | 2125 | StoreValueWide(rl_dest, rl_result); |
| 2126 | } |
| 2127 | |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 2128 | void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 2129 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 2130 | const uint16_t entries = table[1]; |
| 2131 | // Chained cmp-and-branch. |
| 2132 | const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]); |
| 2133 | int32_t current_key = as_int32[0]; |
| 2134 | const int32_t* targets = &as_int32[1]; |
| 2135 | rl_src = LoadValue(rl_src, kCoreReg); |
| 2136 | int i = 0; |
| 2137 | for (; i < entries; i++, current_key++) { |
| 2138 | if (!InexpensiveConstantInt(current_key, Instruction::Code::IF_EQ)) { |
| 2139 | // Switch to using a temp and add. |
| 2140 | break; |
| 2141 | } |
| 2142 | BasicBlock* case_block = |
| 2143 | mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]); |
| 2144 | OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]); |
| 2145 | } |
| 2146 | if (i < entries) { |
| 2147 | // The rest do not seem to be inexpensive. Try to allocate a temp and use add. |
| 2148 | RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false); |
| 2149 | if (key_temp.Valid()) { |
| 2150 | LoadConstantNoClobber(key_temp, current_key); |
| 2151 | for (; i < entries - 1; i++, current_key++) { |
| 2152 | BasicBlock* case_block = |
| 2153 | mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]); |
| 2154 | OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]); |
| 2155 | OpRegImm(kOpAdd, key_temp, 1); // Increment key. |
| 2156 | } |
| 2157 | BasicBlock* case_block = |
| 2158 | mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]); |
| 2159 | OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]); |
| 2160 | } else { |
| 2161 | // No free temp, just finish the old loop. |
| 2162 | for (; i < entries; i++, current_key++) { |
| 2163 | BasicBlock* case_block = |
| 2164 | mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]); |
| 2165 | OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]); |
| 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 2172 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 2173 | if (cu_->verbose) { |
| 2174 | DumpSparseSwitchTable(table); |
| 2175 | } |
| 2176 | |
| 2177 | const uint16_t entries = table[1]; |
| 2178 | if (entries <= kSmallSwitchThreshold) { |
| 2179 | GenSmallPackedSwitch(mir, table_offset, rl_src); |
| 2180 | } else { |
| 2181 | // Use the backend-specific implementation. |
| 2182 | GenLargePackedSwitch(mir, table_offset, rl_src); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 2187 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 2188 | const uint16_t entries = table[1]; |
| 2189 | // Chained cmp-and-branch. |
| 2190 | const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]); |
| 2191 | const int32_t* targets = &keys[entries]; |
| 2192 | rl_src = LoadValue(rl_src, kCoreReg); |
| 2193 | for (int i = 0; i < entries; i++) { |
| 2194 | int key = keys[i]; |
| 2195 | BasicBlock* case_block = |
| 2196 | mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]); |
| 2197 | OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 2202 | const uint16_t* table = mir_graph_->GetTable(mir, table_offset); |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 2203 | if (cu_->verbose) { |
| 2204 | DumpSparseSwitchTable(table); |
| 2205 | } |
| 2206 | |
| 2207 | const uint16_t entries = table[1]; |
| 2208 | if (entries <= kSmallSwitchThreshold) { |
| 2209 | GenSmallSparseSwitch(mir, table_offset, rl_src); |
| 2210 | } else { |
| 2211 | // Use the backend-specific implementation. |
| 2212 | GenLargeSparseSwitch(mir, table_offset, rl_src); |
| 2213 | } |
| 2214 | } |
| 2215 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2216 | bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) { |
| 2217 | switch (size) { |
| 2218 | case kReference: |
| 2219 | return type == Primitive::kPrimNot; |
| 2220 | case k64: |
| 2221 | case kDouble: |
| 2222 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 2223 | case k32: |
| 2224 | case kSingle: |
| 2225 | return type == Primitive::kPrimInt || type == Primitive::kPrimFloat; |
| 2226 | case kSignedHalf: |
| 2227 | return type == Primitive::kPrimShort; |
| 2228 | case kUnsignedHalf: |
| 2229 | return type == Primitive::kPrimChar; |
| 2230 | case kSignedByte: |
| 2231 | return type == Primitive::kPrimByte; |
| 2232 | case kUnsignedByte: |
| 2233 | return type == Primitive::kPrimBoolean; |
| 2234 | case kWord: // Intentional fallthrough. |
| 2235 | default: |
| 2236 | return false; // There are no sane types with this op size. |
| 2237 | } |
| 2238 | } |
| 2239 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2240 | } // namespace art |