blob: 284e8f6c0a522e49c1fd51243fc9439ea44c7eea [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains codegen for the X86 ISA */
18
19#include "codegen_x86.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
21#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex/quick/mir_to_lir-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "driver/compiler_driver.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070024#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010025#include "mirror/art_method.h"
26#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86_lir.h"
28
29namespace art {
30
Brian Carlstrom7940e442013-07-12 13:46:57 -070031/*
32 * The sparse table in the literal pool is an array of <key,displacement>
33 * pairs.
34 */
Andreas Gampe48971b32014-08-06 10:09:01 -070035void X86Mir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -070036 GenSmallSparseSwitch(mir, table_offset, rl_src);
37}
38
39/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 * Code pattern will look something like:
41 *
42 * mov r_val, ..
43 * call 0
44 * pop r_start_of_method
45 * sub r_start_of_method, ..
46 * mov r_key_reg, r_val
47 * sub r_key_reg, low_key
48 * cmp r_key_reg, size-1 ; bound check
49 * ja done
50 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
51 * add r_start_of_method, r_disp
52 * jmp r_start_of_method
53 * done:
54 */
Andreas Gampe48971b32014-08-06 10:09:01 -070055void X86Mir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fu72f53af2014-11-11 16:48:40 -080056 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -070058 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000059 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080060 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 tab_rec->table = table;
62 tab_rec->vaddr = current_dalvik_offset_;
63 int size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010064 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070065
66 // Get the switch value
67 rl_src = LoadValue(rl_src, kCoreReg);
Mark Mendell67c39c42014-01-31 17:28:00 -080068
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -080070 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 // Remove the bias, if necessary
72 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080073 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 } else {
75 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -080076 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 }
Mark Mendell27dee8b2014-12-01 19:06:12 -050078
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 // Bounds check - if < 0 or >= size continue following switch
Serguei Katkov407a9d22014-07-05 03:09:32 +070080 OpRegImm(kOpCmp, keyReg, size - 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 LIR* branch_over = OpCondBranch(kCondHi, NULL);
82
Mark Mendell27dee8b2014-12-01 19:06:12 -050083 RegStorage addr_for_jump;
84 if (cu_->target64) {
85 RegStorage table_base = AllocTempWide();
86 // Load the address of the table into table_base.
87 LIR* lea = RawLIR(current_dalvik_offset_, kX86Lea64RM, table_base.GetReg(), kRIPReg,
88 256, 0, WrapPointer(tab_rec));
89 lea->flags.fixup = kFixupSwitchTable;
90 AppendLIR(lea);
91
92 // Load the offset from the table out of the table.
93 addr_for_jump = AllocTempWide();
94 NewLIR5(kX86MovsxdRA, addr_for_jump.GetReg(), table_base.GetReg(), keyReg.GetReg(), 2, 0);
95
96 // Add the offset from the table to the table base.
97 OpRegReg(kOpAdd, addr_for_jump, table_base);
98 } else {
99 // Materialize a pointer to the switch table.
100 RegStorage start_of_method_reg;
101 if (base_of_code_ != nullptr) {
102 // We can use the saved value.
103 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
104 rl_method = LoadValue(rl_method, kCoreReg);
105 start_of_method_reg = rl_method.reg;
106 store_method_addr_used_ = true;
107 } else {
108 start_of_method_reg = AllocTempRef();
109 NewLIR1(kX86StartOfMethod, start_of_method_reg.GetReg());
110 }
111 // Load the displacement from the switch table.
112 addr_for_jump = AllocTemp();
113 NewLIR5(kX86PcRelLoadRA, addr_for_jump.GetReg(), start_of_method_reg.GetReg(), keyReg.GetReg(),
114 2, WrapPointer(tab_rec));
115 // Add displacement to start of method.
116 OpRegReg(kOpAdd, addr_for_jump, start_of_method_reg);
117 }
118
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 // ..and go!
Mark Mendell27dee8b2014-12-01 19:06:12 -0500120 tab_rec->anchor = NewLIR1(kX86JmpR, addr_for_jump.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121
122 /* branch_over target here */
123 LIR* target = NewLIR0(kPseudoTargetLabel);
124 branch_over->target = target;
125}
126
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700127void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -0700128 int ex_offset = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -0700129 Thread::ExceptionOffset<8>().Int32Value() :
130 Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700131 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Serguei Katkov407a9d22014-07-05 03:09:32 +0700132 NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, rl_result.reg.GetReg(), ex_offset);
133 NewLIR2(cu_->target64 ? kX86Mov64TI : kX86Mov32TI, ex_offset, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 StoreValue(rl_dest, rl_result);
135}
136
Vladimir Markobf535be2014-11-19 18:52:35 +0000137void X86Mir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
Serguei Katkov407a9d22014-07-05 03:09:32 +0700138 DCHECK_EQ(tgt_addr_reg.Is64Bit(), cu_->target64);
Serguei Katkov407a9d22014-07-05 03:09:32 +0700139 RegStorage reg_card_base = AllocTempRef();
140 RegStorage reg_card_no = AllocTempRef();
buzbee33ae5582014-06-12 14:56:32 -0700141 int ct_offset = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -0700142 Thread::CardTableOffset<8>().Int32Value() :
143 Thread::CardTableOffset<4>().Int32Value();
Serguei Katkov407a9d22014-07-05 03:09:32 +0700144 NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, reg_card_base.GetReg(), ct_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800146 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 FreeTemp(reg_card_base);
148 FreeTemp(reg_card_no);
149}
150
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 /*
153 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
154 * allocation mechanism know so it doesn't try to use any of them when
155 * expanding the frame or flushing. This leaves the utility
156 * code with no spare temps.
157 */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800158 const RegStorage arg0 = TargetReg32(kArg0);
159 const RegStorage arg1 = TargetReg32(kArg1);
160 const RegStorage arg2 = TargetReg32(kArg2);
161 LockTemp(arg0);
162 LockTemp(arg1);
163 LockTemp(arg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 /*
166 * We can safely skip the stack overflow check if we're
167 * a leaf *and* our frame size < fudge factor.
168 */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800169 const InstructionSet isa = cu_->target64 ? kX86_64 : kX86;
Dave Allison648d7112014-07-25 16:15:27 -0700170 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, isa);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800171 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Dave Allison69dfe512014-07-11 17:11:58 +0000172
173 // If we doing an implicit stack overflow check, perform the load immediately
174 // before the stack pointer is decremented and anything is saved.
175 if (!skip_overflow_check &&
176 cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
177 // Implicit stack overflow check.
178 // test eax,[esp + -overflow]
179 int overflow = GetStackOverflowReservedBytes(isa);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800180 NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rSP.GetReg(), -overflow);
Dave Allison69dfe512014-07-11 17:11:58 +0000181 MarkPossibleStackOverflowException();
182 }
183
184 /* Build frame, return address already on stack */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800185 stack_decrement_ = OpRegImm(kOpSub, rs_rSP, frame_size_ -
Dave Allison69dfe512014-07-11 17:11:58 +0000186 GetInstructionSetPointerSize(cu_->instruction_set));
187
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 NewLIR0(kPseudoMethodEntry);
189 /* Spill core callee saves */
190 SpillCoreRegs();
Serguei Katkovc3801912014-07-08 17:21:53 +0700191 SpillFPRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700193 class StackOverflowSlowPath : public LIRSlowPath {
194 public:
195 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
196 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
197 }
198 void Compile() OVERRIDE {
199 m2l_->ResetRegPool();
200 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700201 GenerateTargetLabel(kPseudoThrowTarget);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800202 const RegStorage local_rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
203 m2l_->OpRegImm(kOpAdd, local_rs_rSP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700204 m2l_->ClobberCallerSave();
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700205 // Assumes codegen and target are in thumb2 mode.
Andreas Gampe98430592014-07-27 19:44:50 -0700206 m2l_->CallHelper(RegStorage::InvalidReg(), kQuickThrowStackOverflow,
207 false /* MarkSafepointPC */, false /* UseLink */);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700208 }
209
210 private:
211 const size_t sp_displace_;
212 };
Dave Allison69dfe512014-07-11 17:11:58 +0000213 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
214 // TODO: for large frames we should do something like:
215 // spill ebp
216 // lea ebp, [esp + frame_size]
217 // cmp ebp, fs:[stack_end_]
218 // jcc stack_overflow_exception
219 // mov esp, ebp
220 // in case a signal comes in that's not using an alternate signal stack and the large frame
221 // may have moved us outside of the reserved area at the end of the stack.
222 // cmp rs_rX86_SP, fs:[stack_end_]; jcc throw_slowpath
223 if (cu_->target64) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800224 OpRegThreadMem(kOpCmp, rs_rX86_SP_64, Thread::StackEndOffset<8>());
Dave Allison69dfe512014-07-11 17:11:58 +0000225 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800226 OpRegThreadMem(kOpCmp, rs_rX86_SP_32, Thread::StackEndOffset<4>());
Dave Allison69dfe512014-07-11 17:11:58 +0000227 }
228 LIR* branch = OpCondBranch(kCondUlt, nullptr);
229 AddSlowPath(
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700230 new(arena_)StackOverflowSlowPath(this, branch,
231 frame_size_ -
232 GetInstructionSetPointerSize(cu_->instruction_set)));
Dave Allison69dfe512014-07-11 17:11:58 +0000233 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 }
235
236 FlushIns(ArgLocs, rl_method);
237
Mark Mendell67c39c42014-01-31 17:28:00 -0800238 if (base_of_code_ != nullptr) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700239 RegStorage method_start = TargetPtrReg(kArg0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800240 // We have been asked to save the address of the method start for later use.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700241 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, method_start.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -0800242 int displacement = SRegOffset(base_of_code_->s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700243 // Native pointer - must be natural word size.
Ian Rogersb28c1c02014-11-08 11:21:21 -0800244 setup_method_address_[1] = StoreBaseDisp(rs_rSP, displacement, method_start,
Elena Sayapinadd644502014-07-01 18:39:52 +0700245 cu_->target64 ? k64 : k32, kNotVolatile);
Mark Mendell67c39c42014-01-31 17:28:00 -0800246 }
247
Ian Rogersb28c1c02014-11-08 11:21:21 -0800248 FreeTemp(arg0);
249 FreeTemp(arg1);
250 FreeTemp(arg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
253void X86Mir2Lir::GenExitSequence() {
254 /*
255 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
256 * allocated by the register utilities as temps.
257 */
buzbee091cc402014-03-31 10:14:40 -0700258 LockTemp(rs_rX86_RET0);
259 LockTemp(rs_rX86_RET1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260
261 NewLIR0(kPseudoMethodExit);
262 UnSpillCoreRegs();
Serguei Katkovc3801912014-07-08 17:21:53 +0700263 UnSpillFPRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 /* Remove frame except for return address */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800265 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
266 stack_increment_ = OpRegImm(kOpAdd, rs_rSP,
267 frame_size_ - GetInstructionSetPointerSize(cu_->instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 NewLIR0(kX86Ret);
269}
270
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800271void X86Mir2Lir::GenSpecialExitSequence() {
272 NewLIR0(kX86Ret);
273}
274
Dave Allison69dfe512014-07-11 17:11:58 +0000275void X86Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
276 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
277 return;
278 }
279 // Implicit null pointer check.
280 // test eax,[arg1+0]
281 NewLIR3(kX86Test32RM, rs_rAX.GetReg(), reg.GetReg(), 0);
282 MarkPossibleNullPointerException(opt_flags);
283}
284
Vladimir Markof4da6752014-08-01 19:04:18 +0100285/*
286 * Bit of a hack here - in the absence of a real scheduling pass,
287 * emit the next instruction in static & direct invoke sequences.
288 */
289static int X86NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
290 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 uint32_t,
Vladimir Markof4da6752014-08-01 19:04:18 +0100292 uintptr_t direct_code, uintptr_t direct_method,
293 InvokeType type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700294 UNUSED(info, direct_code);
Vladimir Markof4da6752014-08-01 19:04:18 +0100295 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
296 if (direct_method != 0) {
297 switch (state) {
298 case 0: // Get the current Method* [sets kArg0]
299 if (direct_method != static_cast<uintptr_t>(-1)) {
300 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
301 } else {
302 cg->LoadMethodAddress(target_method, type, kArg0);
303 }
304 break;
305 default:
306 return -1;
307 }
308 } else {
309 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
310 switch (state) {
311 case 0: // Get the current Method* [sets kArg0]
312 // TUNING: we can save a reg copy if Method* has been promoted.
313 cg->LoadCurrMethodDirect(arg0_ref);
314 break;
315 case 1: // Get method->dex_cache_resolved_methods_
316 cg->LoadRefDisp(arg0_ref,
317 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
318 arg0_ref,
319 kNotVolatile);
320 break;
321 case 2: // Grab target method*
322 CHECK_EQ(cu->dex_file, target_method.dex_file);
323 cg->LoadRefDisp(arg0_ref,
324 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
325 target_method.dex_method_index).Int32Value(),
326 arg0_ref,
327 kNotVolatile);
328 break;
329 default:
330 return -1;
331 }
332 }
333 return state + 1;
334}
335
336NextCallInsn X86Mir2Lir::GetNextSDCallInsn() {
337 return X86NextSDCallInsn;
338}
339
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340} // namespace art