blob: 61dcc28afc109b5331fe026e1bb853e50abbb880 [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"
20#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010022#include "mirror/art_method.h"
23#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "x86_lir.h"
25
26namespace art {
27
Brian Carlstrom7940e442013-07-12 13:46:57 -070028/*
29 * The sparse table in the literal pool is an array of <key,displacement>
30 * pairs.
31 */
Andreas Gampe48971b32014-08-06 10:09:01 -070032void X86Mir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070033 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070034 if (cu_->verbose) {
35 DumpSparseSwitchTable(table);
36 }
37 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -070038 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
39 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 rl_src = LoadValue(rl_src, kCoreReg);
41 for (int i = 0; i < entries; i++) {
42 int key = keys[i];
43 BasicBlock* case_block =
44 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
buzbee2700f7e2014-03-07 09:46:20 -080045 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 }
47}
48
49/*
50 * Code pattern will look something like:
51 *
52 * mov r_val, ..
53 * call 0
54 * pop r_start_of_method
55 * sub r_start_of_method, ..
56 * mov r_key_reg, r_val
57 * sub r_key_reg, low_key
58 * cmp r_key_reg, size-1 ; bound check
59 * ja done
60 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
61 * add r_start_of_method, r_disp
62 * jmp r_start_of_method
63 * done:
64 */
Andreas Gampe48971b32014-08-06 10:09:01 -070065void X86Mir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070066 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 if (cu_->verbose) {
68 DumpPackedSwitchTable(table);
69 }
70 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -070071 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000072 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 tab_rec->table = table;
74 tab_rec->vaddr = current_dalvik_offset_;
75 int size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070076 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000077 kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010078 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
80 // Get the switch value
81 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070082 // NewLIR0(kX86Bkpt);
Mark Mendell67c39c42014-01-31 17:28:00 -080083
84 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080085 RegStorage start_of_method_reg;
Mark Mendell67c39c42014-01-31 17:28:00 -080086 if (base_of_code_ != nullptr) {
87 // We can use the saved value.
88 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -070089 if (rl_method.wide) {
90 rl_method = LoadValueWide(rl_method, kCoreReg);
91 } else {
92 rl_method = LoadValue(rl_method, kCoreReg);
93 }
buzbee2700f7e2014-03-07 09:46:20 -080094 start_of_method_reg = rl_method.reg;
Mark Mendell55d0eac2014-02-06 11:02:52 -080095 store_method_addr_used_ = true;
Mark Mendell67c39c42014-01-31 17:28:00 -080096 } else {
Serguei Katkov407a9d22014-07-05 03:09:32 +070097 start_of_method_reg = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -080098 NewLIR1(kX86StartOfMethod, start_of_method_reg.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -080099 }
Serguei Katkov407a9d22014-07-05 03:09:32 +0700100 DCHECK_EQ(start_of_method_reg.Is64Bit(), cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800102 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 // Remove the bias, if necessary
104 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800105 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 } else {
107 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800108 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 }
110 // Bounds check - if < 0 or >= size continue following switch
Serguei Katkov407a9d22014-07-05 03:09:32 +0700111 OpRegImm(kOpCmp, keyReg, size - 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 LIR* branch_over = OpCondBranch(kCondHi, NULL);
113
114 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800115 RegStorage disp_reg = AllocTemp();
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700116 NewLIR5(kX86PcRelLoadRA, disp_reg.GetReg(), start_of_method_reg.GetReg(), keyReg.GetReg(),
117 2, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 // Add displacement to start of method
Serguei Katkov407a9d22014-07-05 03:09:32 +0700119 OpRegReg(kOpAdd, start_of_method_reg, cu_->target64 ? As64BitReg(disp_reg) : disp_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 // ..and go!
buzbee2700f7e2014-03-07 09:46:20 -0800121 LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 tab_rec->anchor = switch_branch;
123
124 /* branch_over target here */
125 LIR* target = NewLIR0(kPseudoTargetLabel);
126 branch_over->target = target;
127}
128
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700129void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -0700130 int ex_offset = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -0700131 Thread::ExceptionOffset<8>().Int32Value() :
132 Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700133 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Serguei Katkov407a9d22014-07-05 03:09:32 +0700134 NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, rl_result.reg.GetReg(), ex_offset);
135 NewLIR2(cu_->target64 ? kX86Mov64TI : kX86Mov32TI, ex_offset, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 StoreValue(rl_dest, rl_result);
137}
138
139/*
140 * Mark garbage collection card. Skip if the value we're storing is null.
141 */
buzbee2700f7e2014-03-07 09:46:20 -0800142void X86Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
Serguei Katkov407a9d22014-07-05 03:09:32 +0700143 DCHECK_EQ(tgt_addr_reg.Is64Bit(), cu_->target64);
144 DCHECK_EQ(val_reg.Is64Bit(), cu_->target64);
145 RegStorage reg_card_base = AllocTempRef();
146 RegStorage reg_card_no = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
buzbee33ae5582014-06-12 14:56:32 -0700148 int ct_offset = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -0700149 Thread::CardTableOffset<8>().Int32Value() :
150 Thread::CardTableOffset<4>().Int32Value();
Serguei Katkov407a9d22014-07-05 03:09:32 +0700151 NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, reg_card_base.GetReg(), ct_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800153 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 LIR* target = NewLIR0(kPseudoTargetLabel);
155 branch_over->target = target;
156 FreeTemp(reg_card_base);
157 FreeTemp(reg_card_no);
158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 /*
162 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
163 * allocation mechanism know so it doesn't try to use any of them when
164 * expanding the frame or flushing. This leaves the utility
165 * code with no spare temps.
166 */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800167 const RegStorage arg0 = TargetReg32(kArg0);
168 const RegStorage arg1 = TargetReg32(kArg1);
169 const RegStorage arg2 = TargetReg32(kArg2);
170 LockTemp(arg0);
171 LockTemp(arg1);
172 LockTemp(arg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 /*
175 * We can safely skip the stack overflow check if we're
176 * a leaf *and* our frame size < fudge factor.
177 */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800178 const InstructionSet isa = cu_->target64 ? kX86_64 : kX86;
Dave Allison648d7112014-07-25 16:15:27 -0700179 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, isa);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800180 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Dave Allison69dfe512014-07-11 17:11:58 +0000181
182 // If we doing an implicit stack overflow check, perform the load immediately
183 // before the stack pointer is decremented and anything is saved.
184 if (!skip_overflow_check &&
185 cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
186 // Implicit stack overflow check.
187 // test eax,[esp + -overflow]
188 int overflow = GetStackOverflowReservedBytes(isa);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800189 NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rSP.GetReg(), -overflow);
Dave Allison69dfe512014-07-11 17:11:58 +0000190 MarkPossibleStackOverflowException();
191 }
192
193 /* Build frame, return address already on stack */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800194 stack_decrement_ = OpRegImm(kOpSub, rs_rSP, frame_size_ -
Dave Allison69dfe512014-07-11 17:11:58 +0000195 GetInstructionSetPointerSize(cu_->instruction_set));
196
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197 NewLIR0(kPseudoMethodEntry);
198 /* Spill core callee saves */
199 SpillCoreRegs();
Serguei Katkovc3801912014-07-08 17:21:53 +0700200 SpillFPRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700202 class StackOverflowSlowPath : public LIRSlowPath {
203 public:
204 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
205 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
206 }
207 void Compile() OVERRIDE {
208 m2l_->ResetRegPool();
209 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700210 GenerateTargetLabel(kPseudoThrowTarget);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800211 const RegStorage local_rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
212 m2l_->OpRegImm(kOpAdd, local_rs_rSP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700213 m2l_->ClobberCallerSave();
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700214 // Assumes codegen and target are in thumb2 mode.
Andreas Gampe98430592014-07-27 19:44:50 -0700215 m2l_->CallHelper(RegStorage::InvalidReg(), kQuickThrowStackOverflow,
216 false /* MarkSafepointPC */, false /* UseLink */);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700217 }
218
219 private:
220 const size_t sp_displace_;
221 };
Dave Allison69dfe512014-07-11 17:11:58 +0000222 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
223 // TODO: for large frames we should do something like:
224 // spill ebp
225 // lea ebp, [esp + frame_size]
226 // cmp ebp, fs:[stack_end_]
227 // jcc stack_overflow_exception
228 // mov esp, ebp
229 // in case a signal comes in that's not using an alternate signal stack and the large frame
230 // may have moved us outside of the reserved area at the end of the stack.
231 // cmp rs_rX86_SP, fs:[stack_end_]; jcc throw_slowpath
232 if (cu_->target64) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800233 OpRegThreadMem(kOpCmp, rs_rX86_SP_64, Thread::StackEndOffset<8>());
Dave Allison69dfe512014-07-11 17:11:58 +0000234 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800235 OpRegThreadMem(kOpCmp, rs_rX86_SP_32, Thread::StackEndOffset<4>());
Dave Allison69dfe512014-07-11 17:11:58 +0000236 }
237 LIR* branch = OpCondBranch(kCondUlt, nullptr);
238 AddSlowPath(
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700239 new(arena_)StackOverflowSlowPath(this, branch,
240 frame_size_ -
241 GetInstructionSetPointerSize(cu_->instruction_set)));
Dave Allison69dfe512014-07-11 17:11:58 +0000242 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 }
244
245 FlushIns(ArgLocs, rl_method);
246
Mark Mendell67c39c42014-01-31 17:28:00 -0800247 if (base_of_code_ != nullptr) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700248 RegStorage method_start = TargetPtrReg(kArg0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800249 // We have been asked to save the address of the method start for later use.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700250 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, method_start.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -0800251 int displacement = SRegOffset(base_of_code_->s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700252 // Native pointer - must be natural word size.
Ian Rogersb28c1c02014-11-08 11:21:21 -0800253 setup_method_address_[1] = StoreBaseDisp(rs_rSP, displacement, method_start,
Elena Sayapinadd644502014-07-01 18:39:52 +0700254 cu_->target64 ? k64 : k32, kNotVolatile);
Mark Mendell67c39c42014-01-31 17:28:00 -0800255 }
256
Ian Rogersb28c1c02014-11-08 11:21:21 -0800257 FreeTemp(arg0);
258 FreeTemp(arg1);
259 FreeTemp(arg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260}
261
262void X86Mir2Lir::GenExitSequence() {
263 /*
264 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
265 * allocated by the register utilities as temps.
266 */
buzbee091cc402014-03-31 10:14:40 -0700267 LockTemp(rs_rX86_RET0);
268 LockTemp(rs_rX86_RET1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269
270 NewLIR0(kPseudoMethodExit);
271 UnSpillCoreRegs();
Serguei Katkovc3801912014-07-08 17:21:53 +0700272 UnSpillFPRegs();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 /* Remove frame except for return address */
Ian Rogersb28c1c02014-11-08 11:21:21 -0800274 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
275 stack_increment_ = OpRegImm(kOpAdd, rs_rSP,
276 frame_size_ - GetInstructionSetPointerSize(cu_->instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 NewLIR0(kX86Ret);
278}
279
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800280void X86Mir2Lir::GenSpecialExitSequence() {
281 NewLIR0(kX86Ret);
282}
283
Dave Allison69dfe512014-07-11 17:11:58 +0000284void X86Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
285 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
286 return;
287 }
288 // Implicit null pointer check.
289 // test eax,[arg1+0]
290 NewLIR3(kX86Test32RM, rs_rAX.GetReg(), reg.GetReg(), 0);
291 MarkPossibleNullPointerException(opt_flags);
292}
293
Vladimir Markof4da6752014-08-01 19:04:18 +0100294/*
295 * Bit of a hack here - in the absence of a real scheduling pass,
296 * emit the next instruction in static & direct invoke sequences.
297 */
298static int X86NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
299 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700300 uint32_t,
Vladimir Markof4da6752014-08-01 19:04:18 +0100301 uintptr_t direct_code, uintptr_t direct_method,
302 InvokeType type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700303 UNUSED(info, direct_code);
Vladimir Markof4da6752014-08-01 19:04:18 +0100304 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
305 if (direct_method != 0) {
306 switch (state) {
307 case 0: // Get the current Method* [sets kArg0]
308 if (direct_method != static_cast<uintptr_t>(-1)) {
309 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
310 } else {
311 cg->LoadMethodAddress(target_method, type, kArg0);
312 }
313 break;
314 default:
315 return -1;
316 }
317 } else {
318 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
319 switch (state) {
320 case 0: // Get the current Method* [sets kArg0]
321 // TUNING: we can save a reg copy if Method* has been promoted.
322 cg->LoadCurrMethodDirect(arg0_ref);
323 break;
324 case 1: // Get method->dex_cache_resolved_methods_
325 cg->LoadRefDisp(arg0_ref,
326 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
327 arg0_ref,
328 kNotVolatile);
329 break;
330 case 2: // Grab target method*
331 CHECK_EQ(cu->dex_file, target_method.dex_file);
332 cg->LoadRefDisp(arg0_ref,
333 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
334 target_method.dex_method_index).Int32Value(),
335 arg0_ref,
336 kNotVolatile);
337 break;
338 default:
339 return -1;
340 }
341 }
342 return state + 1;
343}
344
345NextCallInsn X86Mir2Lir::GetNextSDCallInsn() {
346 return X86NextSDCallInsn;
347}
348
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349} // namespace art