blob: ba027f0d07c435060eb9573b63a4e6a16f8a5daf [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
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
17namespace art {
18
19/*
20 * This source files contains "gen" codegen routines that should
21 * be applicable to most targets. Only mid-level support utilities
22 * and "op" calls may be used here.
23 */
24
buzbee31a4a6f2012-02-28 15:36:15 -080025typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -070026 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -070027 uintptr_t directMethod, InvokeType type);
buzbeefc9e6fa2012-03-23 15:14:29 -070028LIR* opCondBranch(CompilationUnit* cUnit, ConditionCode cc, LIR* target);
29
buzbee31a4a6f2012-02-28 15:36:15 -080030/*
31 * If there are any ins passed in registers that have not been promoted
32 * to a callee-save register, flush them to the frame. Perform intial
33 * assignment of promoted arguments.
34 */
35void flushIns(CompilationUnit* cUnit)
36{
buzbee9c044ce2012-03-18 13:24:07 -070037 /*
38 * Dummy up a RegLocation for the incoming Method*
39 * It will attempt to keep rARG0 live (or copy it to home location
40 * if promoted).
41 */
42 RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg];
43 RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg];
44 rlSrc.location = kLocPhysReg;
45 rlSrc.lowReg = rARG0;
46 rlSrc.home = false;
47 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
48 storeValue(cUnit, rlMethod, rlSrc);
49 // If Method* has been promoted, explicitly flush
50 if (rlMethod.location == kLocPhysReg) {
51 storeWordDisp(cUnit, rSP, 0, rARG0);
52 }
53
buzbee31a4a6f2012-02-28 15:36:15 -080054 if (cUnit->numIns == 0)
55 return;
Ian Rogersb3ab25b2012-03-19 01:12:01 -070056 const int numArgRegs = 3;
57 static int argRegs[] = {rARG1, rARG2, rARG3};
buzbee31a4a6f2012-02-28 15:36:15 -080058 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
59 /*
buzbee86a4bce2012-03-06 18:15:00 -080060 * Copy incoming arguments to their proper home locations.
61 * NOTE: an older version of dx had an issue in which
62 * it would reuse static method argument registers.
buzbee31a4a6f2012-02-28 15:36:15 -080063 * This could result in the same Dalvik virtual register
buzbee86a4bce2012-03-06 18:15:00 -080064 * being promoted to both core and fp regs. To account for this,
65 * we only copy to the corresponding promoted physical register
66 * if it matches the type of the SSA name for the incoming
67 * argument. It is also possible that long and double arguments
68 * end up half-promoted. In those cases, we must flush the promoted
69 * half to memory as well.
buzbee31a4a6f2012-02-28 15:36:15 -080070 */
71 for (int i = 0; i < cUnit->numIns; i++) {
buzbee86a4bce2012-03-06 18:15:00 -080072 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
Ian Rogersb3ab25b2012-03-19 01:12:01 -070073 if (i < numArgRegs) {
buzbee31a4a6f2012-02-28 15:36:15 -080074 // If arriving in register
buzbee86a4bce2012-03-06 18:15:00 -080075 bool needFlush = true;
76 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
77 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070078 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080079 needFlush = false;
80 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070081 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080082 needFlush = false;
83 } else {
84 needFlush = true;
buzbee31a4a6f2012-02-28 15:36:15 -080085 }
buzbee86a4bce2012-03-06 18:15:00 -080086
87 // For wide args, force flush if only half is promoted
88 if (tLoc->wide) {
89 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
90 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
91 (pMap->fpLocation != vMap->fpLocation);
buzbee31a4a6f2012-02-28 15:36:15 -080092 }
buzbee86a4bce2012-03-06 18:15:00 -080093 if (needFlush) {
94 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070095 argRegs[i], kWord);
buzbee86a4bce2012-03-06 18:15:00 -080096 }
buzbee31a4a6f2012-02-28 15:36:15 -080097 } else {
98 // If arriving in frame & promoted
buzbee86a4bce2012-03-06 18:15:00 -080099 if (vMap->coreLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800100 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800101 vMap->coreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800102 }
buzbee86a4bce2012-03-06 18:15:00 -0800103 if (vMap->fpLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800104 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800105 vMap->fpReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800106 }
107 }
108 }
109}
110
Ian Rogers3fa13792012-03-18 15:53:45 -0700111void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
112{
113 LIR* curTarget = cUnit->methodLiteralList;
114 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
115 while (curTarget != NULL && nextTarget != NULL) {
116 if (curTarget->operands[0] == (int)dexFile &&
117 nextTarget->operands[0] == (int)dexMethodIdx) {
118 *codeTarget = curTarget;
119 *methodTarget = nextTarget;
120 DCHECK((*codeTarget)->next == *methodTarget);
121 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
122 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
123 break;
124 }
125 curTarget = nextTarget->next;
126 nextTarget = curTarget != NULL ? curTarget->next : NULL;
127 }
128}
129
buzbee31a4a6f2012-02-28 15:36:15 -0800130/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700131 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800132 * emit the next instruction in static & direct invoke sequences.
133 */
134int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700135 int state, uint32_t dexIdx, uint32_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700136 uintptr_t directCode, uintptr_t directMethod,
137 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800138{
Brian Carlstromf5822582012-03-19 22:34:31 -0700139#if !defined(TARGET_ARM)
140 directCode = 0;
141 directMethod = 0;
142#endif
Ian Rogers2ed3b952012-03-17 11:49:39 -0700143 if (directCode != 0 && directMethod != 0) {
144 switch(state) {
145 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700146 if (directCode != (uintptr_t)-1) {
147 loadConstant(cUnit, rINVOKE_TGT, directCode);
148 } else {
149 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
150 if (dataTarget == NULL) {
151 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700152 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700153 }
154#if defined(TARGET_ARM)
155 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
156 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
157 oatAppendLIR(cUnit, loadPcRel);
158#else
159 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
160#endif
161 }
162 if (directMethod != (uintptr_t)-1) {
163 loadConstant(cUnit, rARG0, directMethod);
164 } else {
165 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
166 if (dataTarget == NULL) {
167 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700168 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700169 }
170#if defined(TARGET_ARM)
171 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
172 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
173 oatAppendLIR(cUnit, loadPcRel);
174#else
175 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
176#endif
177 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700178 break;
179 default:
180 return -1;
181 }
182 } else {
183 switch(state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800184 case 0: // Get the current Method* [sets rARG0]
buzbee9c044ce2012-03-18 13:24:07 -0700185 // TUNING: we can save a reg copy if Method* has been promoted
buzbee31a4a6f2012-02-28 15:36:15 -0800186 loadCurrMethodDirect(cUnit, rARG0);
187 break;
188 case 1: // Get method->dex_cache_resolved_methods_
189 loadWordDisp(cUnit, rARG0,
190 Method::DexCacheResolvedMethodsOffset().Int32Value(),
191 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700192 // Set up direct code if known.
193 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700194 if (directCode != (uintptr_t)-1) {
195 loadConstant(cUnit, rINVOKE_TGT, directCode);
196 } else {
197 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
198 if (dataTarget == NULL) {
199 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700200 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700201 }
202#if defined(TARGET_ARM)
203 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
204 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
205 oatAppendLIR(cUnit, loadPcRel);
206#else
207 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
208#endif
209 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700210 }
buzbee31a4a6f2012-02-28 15:36:15 -0800211 break;
212 case 2: // Grab target method*
213 loadWordDisp(cUnit, rARG0,
214 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
215 rARG0);
216 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700217#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800218 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700219 if (directCode == 0) {
220 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
221 rINVOKE_TGT);
222 }
buzbee31a4a6f2012-02-28 15:36:15 -0800223 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700224#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800225 default:
226 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700227 }
buzbee31a4a6f2012-02-28 15:36:15 -0800228 }
229 return state + 1;
230}
231
232/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700233 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800234 * emit the next instruction in a virtual invoke sequence.
235 * We can use rLR as a temp prior to target address loading
236 * Note also that we'll load the first argument ("this") into
237 * rARG1 here rather than the standard loadArgRegs.
238 */
239int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700240 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700241 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800242{
243 RegLocation rlArg;
244 /*
245 * This is the fast path in which the target virtual method is
246 * fully resolved at compile time.
247 */
248 switch(state) {
249 case 0: // Get "this" [set rARG1]
250 rlArg = oatGetSrc(cUnit, mir, 0);
251 loadValueDirectFixed(cUnit, rlArg, rARG1);
252 break;
253 case 1: // Is "this" null? [use rARG1]
254 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800255 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800256 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800257 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800258 break;
buzbee0398c422012-03-02 15:22:47 -0800259 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
260 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
261 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800262 break;
buzbee0398c422012-03-02 15:22:47 -0800263 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
264 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800265 Array::DataOffset(sizeof(Object*)).Int32Value(),
266 rARG0);
267 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700268#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800269 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800270 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800271 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800272 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700273#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800274 default:
275 return -1;
276 }
277 return state + 1;
278}
279
buzbee31a4a6f2012-02-28 15:36:15 -0800280int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
281 int state, uint32_t dexIdx, uint32_t methodIdx)
282{
283 /*
284 * This handles the case in which the base method is not fully
285 * resolved at compile time, we bail to a runtime helper.
286 */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700287#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800288 if (state == 0) {
289 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800290 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800291 // Load rARG0 with method index
292 loadConstant(cUnit, rARG0, dexIdx);
293 return 1;
294 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700295#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800296 return -1;
297}
298
299int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700300 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700301 uintptr_t unused, uintptr_t unused2,
302 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800303{
304 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
305 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
306}
307
308int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700309 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700310 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800311{
312 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
313 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
314}
315
316int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700317 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700318 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800319{
320 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
321 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
322}
323
324int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700325 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700326 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800327{
328 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
329 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
330}
331
332/*
333 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
334 * which will locate the target and continue on via a tail call.
335 */
336int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700337 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700338 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800339{
340 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
341 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
342}
343
344int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
345 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700346 uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700347 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800348{
349 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
350 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
351}
352
353int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
354 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700355 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700356 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800357{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700358#if !defined(TARGET_X86)
359 int lastArgReg = rARG3;
360#else
361 int lastArgReg = rARG2;
362#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800363 int nextReg = rARG1;
364 int nextArg = 0;
365 if (skipThis) {
366 nextReg++;
367 nextArg++;
368 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700369 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800370 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
371 rlArg = oatUpdateRawLoc(cUnit, rlArg);
372 if (rlArg.wide && (nextReg <= rARG2)) {
373 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
374 nextReg++;
375 nextArg++;
376 } else {
377 rlArg.wide = false;
378 loadValueDirectFixed(cUnit, rlArg, nextReg);
379 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700380 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700381 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800382 }
383 return callState;
384}
385
386/*
387 * Load up to 5 arguments, the first three of which will be in
388 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
389 * and as part of the load sequence, it must be replaced with
390 * the target method pointer. Note, this may also be called
391 * for "range" variants if the number of arguments is 5 or fewer.
392 */
393int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
394 DecodedInstruction* dInsn, int callState,
395 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700396 uint32_t dexIdx, uint32_t methodIdx,
397 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700398 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800399{
400 RegLocation rlArg;
401
402 /* If no arguments, just return */
403 if (dInsn->vA == 0)
404 return callState;
405
Ian Rogers2ed3b952012-03-17 11:49:39 -0700406 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700407 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800408
409 DCHECK_LE(dInsn->vA, 5U);
410 if (dInsn->vA > 3) {
411 uint32_t nextUse = 3;
412 //Detect special case of wide arg spanning arg3/arg4
413 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
414 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
415 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
416 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
417 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700418 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800419 // Wide spans, we need the 2nd half of uses[2].
420 rlArg = oatUpdateLocWide(cUnit, rlUse2);
421 if (rlArg.location == kLocPhysReg) {
422 reg = rlArg.highReg;
423 } else {
424 // rARG2 & rARG3 can safely be used here
425 reg = rARG3;
426 loadWordDisp(cUnit, rSP,
427 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
428 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700429 methodIdx, directCode, directMethod,
430 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800431 }
432 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
433 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700434 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700435 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800436 nextUse++;
437 }
438 // Loop through the rest
439 while (nextUse < dInsn->vA) {
440 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700441 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800442 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
443 rlArg = oatUpdateRawLoc(cUnit, rlArg);
444 if (rlArg.location == kLocPhysReg) {
445 lowReg = rlArg.lowReg;
446 highReg = rlArg.highReg;
447 } else {
448 lowReg = rARG2;
buzbee31a4a6f2012-02-28 15:36:15 -0800449 if (rlArg.wide) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700450 highReg = rARG3;
buzbee31a4a6f2012-02-28 15:36:15 -0800451 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
452 } else {
453 loadValueDirectFixed(cUnit, rlArg, lowReg);
454 }
455 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700456 methodIdx, directCode, directMethod,
457 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800458 }
459 int outsOffset = (nextUse + 1) * 4;
460 if (rlArg.wide) {
461 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
462 nextUse += 2;
463 } else {
464 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
465 nextUse++;
466 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700467 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700468 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800469 }
470 }
471
472 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700473 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700474 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800475
476 if (pcrLabel) {
477 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
478 }
479 return callState;
480}
481
482/*
483 * May have 0+ arguments (also used for jumbo). Note that
484 * source virtual registers may be in physical registers, so may
485 * need to be flushed to home location before copying. This
486 * applies to arg3 and above (see below).
487 *
488 * Two general strategies:
489 * If < 20 arguments
490 * Pass args 3-18 using vldm/vstm block copy
491 * Pass arg0, arg1 & arg2 in rARG1-rARG3
492 * If 20+ arguments
493 * Pass args arg19+ using memcpy block copy
494 * Pass arg0, arg1 & arg2 in rARG1-rARG3
495 *
496 */
497int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
498 DecodedInstruction* dInsn, int callState,
499 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700500 uint32_t dexIdx, uint32_t methodIdx,
501 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700502 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800503{
504 int firstArg = dInsn->vC;
505 int numArgs = dInsn->vA;
506
507 // If we can treat it as non-range (Jumbo ops will use range form)
508 if (numArgs <= 5)
509 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
510 nextCallInsn, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700511 directCode, directMethod, type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800512 /*
513 * Make sure range list doesn't span the break between in normal
514 * Dalvik vRegs and the ins.
515 */
516 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
517 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
518 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
519 LOG(FATAL) << "Argument list spanned locals & args";
520 }
521
522 /*
523 * First load the non-register arguments. Both forms expect all
524 * of the source arguments to be in their home frame location, so
525 * scan the sReg names and flush any that have been promoted to
526 * frame backing storage.
527 */
528 // Scan the rest of the args - if in physReg flush to memory
529 for (int nextArg = 0; nextArg < numArgs;) {
530 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
531 if (loc.wide) {
532 loc = oatUpdateLocWide(cUnit, loc);
533 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
534 storeBaseDispWide(cUnit, rSP,
535 oatSRegOffset(cUnit, loc.sRegLow),
536 loc.lowReg, loc.highReg);
537 }
538 nextArg += 2;
539 } else {
540 loc = oatUpdateLoc(cUnit, loc);
541 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
542 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
543 loc.lowReg, kWord);
544 }
545 nextArg++;
546 }
547 }
548
549 int startOffset = oatSRegOffset(cUnit,
550 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
551 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700552#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800553 // Generate memcpy
554 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
555 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700556 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
557 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800558#else
559 if (numArgs >= 20) {
560 // Generate memcpy
561 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
562 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700563 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
564 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800565 } else {
566 // Use vldm/vstm pair using rARG3 as a temp
567 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700568 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700569 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800570 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
571 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
572 //TUNING: loosen barrier
573 ld->defMask = ENCODE_ALL;
574 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700575 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700576 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800577 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700578 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700579 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800580 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
581 setMemRefType(st, false /* isLoad */, kDalvikReg);
582 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700583 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700584 directCode, directMethod, type);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700585
buzbee31a4a6f2012-02-28 15:36:15 -0800586 }
587#endif
588
589 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700590 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700591 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800592
Ian Rogers2ed3b952012-03-17 11:49:39 -0700593 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700594 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800595 if (pcrLabel) {
596 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
597 }
598 return callState;
599}
600
buzbeefc9e6fa2012-03-23 15:14:29 -0700601RegLocation inlineTarget(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir)
602{
603 RegLocation res;
604 mir = oatFindMoveResult(cUnit, bb, mir, false);
605 if (mir == NULL) {
606 res = oatGetReturn(cUnit, false);
607 } else {
608 res = oatGetDest(cUnit, mir, 0);
609 mir->dalvikInsn.opcode = Instruction::NOP;
610 }
611 return res;
612}
613
614RegLocation inlineTargetWide(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir)
615{
616 RegLocation res;
617 mir = oatFindMoveResult(cUnit, bb, mir, true);
618 if (mir == NULL) {
619 res = oatGetReturnWide(cUnit, false);
620 } else {
621 res = oatGetDestWide(cUnit, mir, 0, 1);
622 mir->dalvikInsn.opcode = Instruction::NOP;
623 }
624 return res;
625}
626
627bool genInlinedCharAt(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
628 InvokeType type, bool isRange)
629{
630#if defined(TARGET_ARM)
631 // Location of reference to data array
632 int valueOffset = String::ValueOffset().Int32Value();
633 // Location of count
634 int countOffset = String::CountOffset().Int32Value();
635 // Starting offset within data array
636 int offsetOffset = String::OffsetOffset().Int32Value();
637 // Start of char data with array_
638 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
639
640 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
641 RegLocation rlIdx = oatGetSrc(cUnit, mir, 1);
642 rlObj = loadValue(cUnit, rlObj, kCoreReg);
643 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
644 int regMax;
645 int regOff = oatAllocTemp(cUnit);
646 int regPtr = oatAllocTemp(cUnit);
647 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);
648 bool rangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
649 if (rangeCheck) {
650 regMax = oatAllocTemp(cUnit);
651 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
652 }
653 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
654 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
655 LIR* launchPad = NULL;
656 if (rangeCheck) {
657 // Set up a launch pad to allow retry in case of bounds violation */
658 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
659 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
660 (intptr_t)launchPad);
661 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
662 oatFreeTemp(cUnit, regMax);
663 opCondBranch(cUnit, kCondCs, launchPad);
664 }
665 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
666 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
667 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
668 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
669 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
670 oatFreeTemp(cUnit, regOff);
671 oatFreeTemp(cUnit, regPtr);
672 storeValue(cUnit, rlDest, rlResult);
673 if (rangeCheck) {
674 launchPad->operands[2] = NULL; // no resumption
675 launchPad->operands[3] = (uintptr_t)bb;
676 }
677 // Record that we've already inlined & null checked
678 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
679 return true;
680#else
681 return false;
682#endif
683}
684
685bool genInlinedMinMaxInt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir,
686 bool isMin)
687{
688#if defined(TARGET_ARM)
689 RegLocation rlSrc1 = oatGetSrc(cUnit, mir, 0);
690 RegLocation rlSrc2 = oatGetSrc(cUnit, mir, 1);
691 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
692 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
693 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
694 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
695 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
696 opIT(cUnit, (isMin) ? kArmCondGt : kArmCondLt, "E");
697 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
698 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
699 genBarrier(cUnit);
700 storeValue(cUnit, rlDest, rlResult);
701 return true;
702#else
703 return false;
704#endif
705}
706
707// Generates an inlined String.isEmpty or String.length.
708bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit,
709 BasicBlock* bb, MIR* mir,
710 bool isEmpty)
711{
712#if defined(TARGET_ARM)
713 // dst = src.length();
714 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
715 rlObj = loadValue(cUnit, rlObj, kCoreReg);
716 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
717 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
718 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);
719 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
720 rlResult.lowReg);
721 if (isEmpty) {
722 // dst = (dst == 0);
723 int tReg = oatAllocTemp(cUnit);
724 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
725 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
726 }
727 storeValue(cUnit, rlDest, rlResult);
728 return true;
729#else
730 return false;
731#endif
732}
733
734bool genInlinedAbsInt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
735{
736#if defined(TARGET_ARM)
737 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
738 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
739 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
740 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
741 int signReg = oatAllocTemp(cUnit);
742 // abs(x) = y<=x>>31, (x+y)^y.
743 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
744 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
745 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
746 storeValue(cUnit, rlDest, rlResult);
747 return true;
748#else
749 return false;
750#endif
751}
752
753bool genInlinedAbsLong(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
754{
755#if defined(TARGET_ARM)
756 RegLocation rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
757 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
758 RegLocation rlDest = inlineTargetWide(cUnit, bb, mir);
759 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
760 int signReg = oatAllocTemp(cUnit);
761 // abs(x) = y<=x>>31, (x+y)^y.
762 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
763 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
764 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
765 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
766 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
767 storeValueWide(cUnit, rlDest, rlResult);
768 return true;
769#else
770 return false;
771#endif
772}
773
774bool genInlinedFloatCvt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
775{
776#if defined(TARGET_ARM)
777 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
778 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
779 storeValue(cUnit, rlDest, rlSrc);
780 return true;
781#else
782 return false;
783#endif
784}
785
786bool genInlinedDoubleCvt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
787{
788#if defined(TARGET_ARM)
789 RegLocation rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
790 RegLocation rlDest = inlineTargetWide(cUnit, bb, mir);
791 storeValueWide(cUnit, rlDest, rlSrc);
792 return true;
793#else
794 return false;
795#endif
796}
797
798/*
799 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
800 * otherwise bails to standard library code.
801 */
802bool genInlinedIndexOf(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
803 InvokeType type, bool zeroBased)
804{
805#if defined(TARGET_ARM)
806
807 oatClobberCalleeSave(cUnit);
808 oatLockCallTemps(cUnit); // Using fixed registers
809 int regPtr = rARG0;
810 int regChar = rARG1;
811 int regStart = rARG2;
812
813 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
814 RegLocation rlChar = oatGetSrc(cUnit, mir, 1);
815 RegLocation rlStart = oatGetSrc(cUnit, mir, 2);
816 loadValueDirectFixed(cUnit, rlObj, regPtr);
817 loadValueDirectFixed(cUnit, rlChar, regChar);
818 if (zeroBased) {
819 loadConstant(cUnit, regStart, 0);
820 } else {
821 loadValueDirectFixed(cUnit, rlStart, regStart);
822 }
823 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pIndexOf));
824 genNullCheck(cUnit, rlObj.sRegLow, regPtr, mir);
825 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
826 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
827 (intptr_t)launchPad);
828 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
829 opReg(cUnit, kOpBlx, rTgt);
830 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
831 launchPad->operands[2] = (uintptr_t)resumeTgt;
832 launchPad->operands[3] = (uintptr_t)bb;
833 // Record that we've already inlined & null checked
834 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
835 return true;
836#else
837 return false;
838#endif
839}
840
841/* Fast string.compareTo(Ljava/lang/string;)I. */
842bool genInlinedStringCompareTo(CompilationUnit* cUnit, BasicBlock* bb,
843 MIR* mir, InvokeType type)
844{
845#if defined(TARGET_ARM)
846 oatClobberCalleeSave(cUnit);
847 oatLockCallTemps(cUnit); // Using fixed registers
848 int regThis = rARG0;
849 int regCmp = rARG1;
850
851 RegLocation rlThis = oatGetSrc(cUnit, mir, 0);
852 RegLocation rlCmp = oatGetSrc(cUnit, mir, 1);
853 loadValueDirectFixed(cUnit, rlThis, regThis);
854 loadValueDirectFixed(cUnit, rlCmp, regCmp);
855 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pStringCompareTo));
856 genNullCheck(cUnit, rlThis.sRegLow, regThis, mir);
857 //TUNING: check if rlCmp.sRegLow is already null checked
858 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
859 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
860 (intptr_t)launchPad);
861 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
862 opReg(cUnit, kOpBlx, rTgt);
863 launchPad->operands[2] = NULL; // No return possible
864 launchPad->operands[3] = (uintptr_t)bb;
865 // Record that we've already inlined & null checked
866 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
867 return true;
868#else
869 return false;
870#endif
871}
872
873bool genIntrinsic(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
874 InvokeType type, bool isRange)
875{
876 if ((mir->optimizationFlags & MIR_INLINED) || isRange) {
877 return false;
878 }
879 /*
880 * TODO: move these to a target-specific structured constant array
881 * and use a generic match function. The list of intrinsics may be
882 * slightly different depending on target.
883 * TODO: Fold this into a matching function that runs during
884 * basic block building. This should be part of the action for
885 * small method inlining and recognition of the special object init
886 * method. By doing this during basic block construction, we can also
887 * take advantage of/generate new useful dataflow info.
888 */
889 std::string tgtMethod = PrettyMethod(mir->dalvikInsn.vB, *cUnit->dex_file);
890 if (tgtMethod.compare("char java.lang.String.charAt(int)") == 0) {
891 return genInlinedCharAt(cUnit, bb, mir, type, isRange);
892 }
893 if (tgtMethod.compare("int java.lang.Math.min(int, int)") == 0) {
894 return genInlinedMinMaxInt(cUnit, bb, mir, true /* isMin */);
895 }
896 if (tgtMethod.compare("int java.lang.Math.max(int, int)") == 0) {
897 return genInlinedMinMaxInt(cUnit, bb, mir, false /* isMin */);
898 }
899 if (tgtMethod.compare("int java.lang.String.length()") == 0) {
900 return genInlinedStringIsEmptyOrLength(cUnit, bb, mir, false /* isEmpty */);
901 }
902 if (tgtMethod.compare("boolean java.lang.String.isEmpty()") == 0) {
903 return genInlinedStringIsEmptyOrLength(cUnit, bb, mir, true /* isEmpty */);
904 }
905 if (tgtMethod.compare("int java.lang.Math.abs(int)") == 0) {
906 return genInlinedAbsInt(cUnit, bb, mir);
907 }
908 if (tgtMethod.compare("long java.lang.Math.abs(long)") == 0) {
909 return genInlinedAbsLong(cUnit, bb, mir);
910 }
911 if (tgtMethod.compare("int java.lang.Float.floatToRawIntBits(float)") == 0) {
912 return genInlinedFloatCvt(cUnit, bb, mir);
913 }
914 if (tgtMethod.compare("float java.lang.Float.intBitsToFloat(int)") == 0) {
915 return genInlinedFloatCvt(cUnit, bb, mir);
916 }
917 if (tgtMethod.compare("long java.lang.Double.doubleToRawLongBits(double)") == 0) {
918 return genInlinedDoubleCvt(cUnit, bb, mir);
919 }
920 if (tgtMethod.compare("double java.lang.Double.longBitsToDouble(long)") == 0) {
921 return genInlinedDoubleCvt(cUnit, bb, mir);
922 }
923 if (tgtMethod.compare("int java.lang.String.indexOf(int, int)") == 0) {
924 return genInlinedIndexOf(cUnit, bb, mir, type, false /* base 0 */);
925 }
926 if (tgtMethod.compare("int java.lang.String.indexOf(int)") == 0) {
927 return genInlinedIndexOf(cUnit, bb, mir, type, true /* base 0 */);
928 }
929 if (tgtMethod.compare("int java.lang.String.compareTo(java.lang.String)") == 0) {
930 return genInlinedStringCompareTo(cUnit, bb, mir, type);
931 }
932 return false;
933}
934
935
buzbee31a4a6f2012-02-28 15:36:15 -0800936} // namespace art