blob: c75e5fe238d5aea8fb7b79fdbb00f5354b395a63 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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#include "Dalvik.h"
18#include "CompilerInternals.h"
19
20static ArenaMemBlock *arenaHead, *currentArena;
21static int numArenaBlocks;
22
23/* Allocate the initial memory block for arena-based allocation */
24bool dvmCompilerHeapInit(void)
25{
26 assert(arenaHead == NULL);
27 arenaHead =
28 (ArenaMemBlock *) malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE);
29 if (arenaHead == NULL) {
30 LOGE("No memory left to create compiler heap memory\n");
31 return false;
32 }
Ben Cheng7a2697d2010-06-07 13:44:23 -070033 arenaHead->blockSize = ARENA_DEFAULT_SIZE;
Ben Chengba4fc8b2009-06-01 13:00:29 -070034 currentArena = arenaHead;
35 currentArena->bytesAllocated = 0;
36 currentArena->next = NULL;
37 numArenaBlocks = 1;
38
39 return true;
40}
41
42/* Arena-based malloc for compilation tasks */
43void * dvmCompilerNew(size_t size, bool zero)
44{
45 size = (size + 3) & ~3;
46retry:
47 /* Normal case - space is available in the current page */
Ben Cheng7a2697d2010-06-07 13:44:23 -070048 if (size + currentArena->bytesAllocated <= currentArena->blockSize) {
Ben Chengba4fc8b2009-06-01 13:00:29 -070049 void *ptr;
50 ptr = &currentArena->ptr[currentArena->bytesAllocated];
51 currentArena->bytesAllocated += size;
52 if (zero) {
53 memset(ptr, 0, size);
54 }
55 return ptr;
56 } else {
57 /*
58 * See if there are previously allocated arena blocks before the last
59 * reset
60 */
61 if (currentArena->next) {
62 currentArena = currentArena->next;
63 goto retry;
64 }
Ben Cheng7a2697d2010-06-07 13:44:23 -070065
66 size_t blockSize = (size < ARENA_DEFAULT_SIZE) ?
67 ARENA_DEFAULT_SIZE : size;
Ben Chengba4fc8b2009-06-01 13:00:29 -070068 /* Time to allocate a new arena */
69 ArenaMemBlock *newArena = (ArenaMemBlock *)
Ben Cheng7a2697d2010-06-07 13:44:23 -070070 malloc(sizeof(ArenaMemBlock) + blockSize);
71 if (newArena == NULL) {
72 LOGE("Arena allocation failure");
73 dvmAbort();
74 }
75 newArena->blockSize = blockSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -070076 newArena->bytesAllocated = 0;
77 newArena->next = NULL;
78 currentArena->next = newArena;
79 currentArena = newArena;
80 numArenaBlocks++;
Ben Cheng60c24f42010-01-04 12:29:56 -080081 if (numArenaBlocks > 10)
Ben Chenga4973592010-03-31 11:59:18 -070082 LOGI("Total arena pages for JIT: %d", numArenaBlocks);
Ben Chengba4fc8b2009-06-01 13:00:29 -070083 goto retry;
84 }
Ben Cheng00603072010-10-28 11:13:58 -070085 /* Should not reach here */
86 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -070087}
88
89/* Reclaim all the arena blocks allocated so far */
90void dvmCompilerArenaReset(void)
91{
92 ArenaMemBlock *block;
93
94 for (block = arenaHead; block; block = block->next) {
95 block->bytesAllocated = 0;
96 }
97 currentArena = arenaHead;
98}
99
100/* Growable List initialization */
101void dvmInitGrowableList(GrowableList *gList, size_t initLength)
102{
103 gList->numAllocated = initLength;
104 gList->numUsed = 0;
Ben Cheng00603072010-10-28 11:13:58 -0700105 gList->elemList = (intptr_t *) dvmCompilerNew(sizeof(intptr_t) * initLength,
106 true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700107}
108
109/* Expand the capacity of a growable list */
110static void expandGrowableList(GrowableList *gList)
111{
112 int newLength = gList->numAllocated;
113 if (newLength < 128) {
114 newLength <<= 1;
115 } else {
116 newLength += 128;
117 }
Ben Cheng00603072010-10-28 11:13:58 -0700118 intptr_t *newArray =
119 (intptr_t *) dvmCompilerNew(sizeof(intptr_t) * newLength, true);
120 memcpy(newArray, gList->elemList, sizeof(intptr_t) * gList->numAllocated);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700121 gList->numAllocated = newLength;
Ben Cheng00603072010-10-28 11:13:58 -0700122 gList->elemList = newArray;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700123}
124
125/* Insert a new element into the growable list */
Ben Cheng00603072010-10-28 11:13:58 -0700126void dvmInsertGrowableList(GrowableList *gList, intptr_t elem)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700127{
Ben Cheng7a2697d2010-06-07 13:44:23 -0700128 assert(gList->numAllocated != 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700129 if (gList->numUsed == gList->numAllocated) {
130 expandGrowableList(gList);
131 }
132 gList->elemList[gList->numUsed++] = elem;
133}
134
Ben Cheng00603072010-10-28 11:13:58 -0700135void dvmGrowableListIteratorInit(GrowableList *gList,
136 GrowableListIterator *iterator)
137{
138 iterator->list = gList;
139 iterator->idx = 0;
140 iterator->size = gList->numUsed;
141}
142
143intptr_t dvmGrowableListIteratorNext(GrowableListIterator *iterator)
144{
145 assert(iterator->size == iterator->list->numUsed);
146 if (iterator->idx == iterator->size) return 0;
147 return iterator->list->elemList[iterator->idx++];
148}
149
150intptr_t dvmGrowableListGetElement(const GrowableList *gList, size_t idx)
151{
152 assert(idx < gList->numUsed);
153 return gList->elemList[idx];
154}
155
Ben Chengba4fc8b2009-06-01 13:00:29 -0700156/* Debug Utility - dump a compilation unit */
157void dvmCompilerDumpCompilationUnit(CompilationUnit *cUnit)
158{
Ben Chengba4fc8b2009-06-01 13:00:29 -0700159 BasicBlock *bb;
Carl Shapiro5d5b94c2011-04-19 17:34:24 -0700160 const char *blockTypeNames[] = {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700161 "Normal Chaining Cell",
162 "Hot Chaining Cell",
163 "Singleton Chaining Cell",
164 "Predicted Chaining Cell",
165 "Backward Branch",
166 "Chaining Cell Gap",
167 "N/A",
Ben Cheng32115a92011-03-22 14:09:09 -0700168 "Entry Block",
Ben Cheng7a2697d2010-06-07 13:44:23 -0700169 "Code Block",
Ben Cheng32115a92011-03-22 14:09:09 -0700170 "Exit Block",
Ben Cheng7a2697d2010-06-07 13:44:23 -0700171 "PC Reconstruction",
172 "Exception Handling",
173 };
174
175 LOGD("Compiling %s %s", cUnit->method->clazz->descriptor,
176 cUnit->method->name);
177 LOGD("%d insns", dvmGetMethodInsnsSize(cUnit->method));
178 LOGD("%d blocks in total", cUnit->numBlocks);
Ben Cheng00603072010-10-28 11:13:58 -0700179 GrowableListIterator iterator;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700180
Ben Cheng00603072010-10-28 11:13:58 -0700181 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
182
183 while (true) {
184 bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
185 if (bb == NULL) break;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700186 LOGD("Block %d (%s) (insn %04x - %04x%s)\n",
187 bb->id,
188 blockTypeNames[bb->blockType],
189 bb->startOffset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700190 bb->lastMIRInsn ? bb->lastMIRInsn->offset : bb->startOffset,
191 bb->lastMIRInsn ? "" : " empty");
192 if (bb->taken) {
193 LOGD(" Taken branch: block %d (%04x)\n",
194 bb->taken->id, bb->taken->startOffset);
195 }
196 if (bb->fallThrough) {
197 LOGD(" Fallthrough : block %d (%04x)\n",
198 bb->fallThrough->id, bb->fallThrough->startOffset);
199 }
200 }
201}
202
203/*
Ben Cheng8b258bf2009-06-24 17:27:07 -0700204 * dvmHashForeach callback.
205 */
206static int dumpMethodStats(void *compilerMethodStats, void *totalMethodStats)
207{
208 CompilerMethodStats *methodStats =
209 (CompilerMethodStats *) compilerMethodStats;
210 CompilerMethodStats *totalStats =
211 (CompilerMethodStats *) totalMethodStats;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700212
213 totalStats->dalvikSize += methodStats->dalvikSize;
214 totalStats->compiledDalvikSize += methodStats->compiledDalvikSize;
215 totalStats->nativeSize += methodStats->nativeSize;
216
Ben Chenge80cd942009-07-17 15:54:23 -0700217 /* Enable the following when fine-tuning the JIT performance */
218#if 0
Ben Cheng8b258bf2009-06-24 17:27:07 -0700219 int limit = (methodStats->dalvikSize >> 2) * 3;
220
221 /* If over 3/4 of the Dalvik code is compiled, print something */
222 if (methodStats->compiledDalvikSize >= limit) {
223 LOGD("Method stats: %s%s, %d/%d (compiled/total Dalvik), %d (native)",
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700224 methodStats->method->clazz->descriptor,
225 methodStats->method->name,
Ben Cheng8b258bf2009-06-24 17:27:07 -0700226 methodStats->compiledDalvikSize,
227 methodStats->dalvikSize,
228 methodStats->nativeSize);
229 }
Ben Chenge80cd942009-07-17 15:54:23 -0700230#endif
Ben Cheng8b258bf2009-06-24 17:27:07 -0700231 return 0;
232}
233
234/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700235 * Dump the current stats of the compiler, including number of bytes used in
236 * the code cache, arena size, and work queue length, and various JIT stats.
237 */
238void dvmCompilerDumpStats(void)
239{
Ben Cheng8b258bf2009-06-24 17:27:07 -0700240 CompilerMethodStats totalMethodStats;
241
242 memset(&totalMethodStats, 0, sizeof(CompilerMethodStats));
243 LOGD("%d compilations using %d + %d bytes",
244 gDvmJit.numCompilations,
245 gDvmJit.templateSize,
246 gDvmJit.codeCacheByteUsed - gDvmJit.templateSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700247 LOGD("Compiler arena uses %d blocks (%d bytes each)",
248 numArenaBlocks, ARENA_DEFAULT_SIZE);
249 LOGD("Compiler work queue length is %d/%d", gDvmJit.compilerQueueLength,
250 gDvmJit.compilerMaxQueued);
251 dvmJitStats();
252 dvmCompilerArchDump();
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700253 if (gDvmJit.methodStatsTable) {
254 dvmHashForeach(gDvmJit.methodStatsTable, dumpMethodStats,
255 &totalMethodStats);
Ben Cheng978738d2010-05-13 13:45:57 -0700256 LOGD("Code size stats: %d/%d (compiled/total Dalvik), %d (native)",
257 totalMethodStats.compiledDalvikSize,
258 totalMethodStats.dalvikSize,
259 totalMethodStats.nativeSize);
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700260 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700261}
Ben Cheng4238ec22009-08-24 16:32:22 -0700262
263/*
264 * Allocate a bit vector with enough space to hold at least the specified
265 * number of bits.
266 *
267 * NOTE: this is the sister implementation of dvmAllocBitVector. In this version
268 * memory is allocated from the compiler arena.
269 */
Andy McFadden9fd527f2010-12-10 15:34:00 -0800270BitVector* dvmCompilerAllocBitVector(unsigned int startBits, bool expandable)
Ben Cheng4238ec22009-08-24 16:32:22 -0700271{
272 BitVector* bv;
Andy McFadden9fd527f2010-12-10 15:34:00 -0800273 unsigned int count;
Ben Cheng4238ec22009-08-24 16:32:22 -0700274
275 assert(sizeof(bv->storage[0]) == 4); /* assuming 32-bit units */
Ben Cheng4238ec22009-08-24 16:32:22 -0700276
277 bv = (BitVector*) dvmCompilerNew(sizeof(BitVector), false);
278
279 count = (startBits + 31) >> 5;
280
281 bv->storageSize = count;
282 bv->expandable = expandable;
283 bv->storage = (u4*) dvmCompilerNew(count * sizeof(u4), true);
284 return bv;
285}
286
287/*
288 * Mark the specified bit as "set".
289 *
290 * Returns "false" if the bit is outside the range of the vector and we're
291 * not allowed to expand.
292 *
293 * NOTE: this is the sister implementation of dvmSetBit. In this version
294 * memory is allocated from the compiler arena.
295 */
Andy McFadden9fd527f2010-12-10 15:34:00 -0800296bool dvmCompilerSetBit(BitVector *pBits, unsigned int num)
Ben Cheng4238ec22009-08-24 16:32:22 -0700297{
Andy McFadden9fd527f2010-12-10 15:34:00 -0800298 if (num >= pBits->storageSize * sizeof(u4) * 8) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700299 if (!pBits->expandable)
Ben Cheng00603072010-10-28 11:13:58 -0700300 dvmAbort();
Ben Cheng4238ec22009-08-24 16:32:22 -0700301
Ben Chengabf3ef82010-11-23 11:55:16 -0800302 /* Round up to word boundaries for "num+1" bits */
Andy McFadden9fd527f2010-12-10 15:34:00 -0800303 unsigned int newSize = (num + 1 + 31) >> 5;
Ben Cheng4238ec22009-08-24 16:32:22 -0700304 assert(newSize > pBits->storageSize);
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800305 u4 *newStorage = (u4*)dvmCompilerNew(newSize * sizeof(u4), false);
Ben Cheng4238ec22009-08-24 16:32:22 -0700306 memcpy(newStorage, pBits->storage, pBits->storageSize * sizeof(u4));
307 memset(&newStorage[pBits->storageSize], 0,
308 (newSize - pBits->storageSize) * sizeof(u4));
309 pBits->storage = newStorage;
310 pBits->storageSize = newSize;
311 }
312
313 pBits->storage[num >> 5] |= 1 << (num & 0x1f);
314 return true;
315}
316
Ben Cheng00603072010-10-28 11:13:58 -0700317/*
318 * Mark the specified bit as "unset".
319 *
320 * Returns "false" if the bit is outside the range of the vector and we're
321 * not allowed to expand.
322 *
323 * NOTE: this is the sister implementation of dvmClearBit. In this version
324 * memory is allocated from the compiler arena.
325 */
Andy McFadden9fd527f2010-12-10 15:34:00 -0800326bool dvmCompilerClearBit(BitVector *pBits, unsigned int num)
Ben Cheng00603072010-10-28 11:13:58 -0700327{
Andy McFadden9fd527f2010-12-10 15:34:00 -0800328 if (num >= pBits->storageSize * sizeof(u4) * 8) {
Ben Cheng00603072010-10-28 11:13:58 -0700329 LOGE("Trying to clear a bit that is not set in the vector yet!");
330 dvmAbort();
331 }
332
333 pBits->storage[num >> 5] &= ~(1 << (num & 0x1f));
334 return true;
335}
336
337/*
338 * If set is true, mark all bits as 1. Otherwise mark all bits as 0.
339 */
340void dvmCompilerMarkAllBits(BitVector *pBits, bool set)
341{
342 int value = set ? -1 : 0;
343 memset(pBits->storage, value, pBits->storageSize * (int)sizeof(u4));
344}
345
Ben Cheng4238ec22009-08-24 16:32:22 -0700346void dvmDebugBitVector(char *msg, const BitVector *bv, int length)
347{
348 int i;
349
350 LOGE("%s", msg);
351 for (i = 0; i < length; i++) {
352 if (dvmIsBitSet(bv, i)) {
Ben Cheng46cd4fb2011-03-16 17:19:06 -0700353 LOGE(" Bit %d is set", i);
Ben Cheng4238ec22009-08-24 16:32:22 -0700354 }
355 }
356}
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800357
358void dvmCompilerAbort(CompilationUnit *cUnit)
359{
360 LOGE("Jit: aborting trace compilation, reverting to interpreter");
361 /* Force a traceback in debug builds */
362 assert(0);
363 /*
364 * Abort translation and force to interpret-only for this trace
365 * Matching setjmp in compiler thread work loop in Compiler.c.
366 */
367 longjmp(*cUnit->bailPtr, 1);
368}
Ben Cheng00603072010-10-28 11:13:58 -0700369
370void dvmDumpBlockBitVector(const GrowableList *blocks, char *msg,
371 const BitVector *bv, int length)
372{
373 int i;
374
375 LOGE("%s", msg);
376 for (i = 0; i < length; i++) {
377 if (dvmIsBitSet(bv, i)) {
378 BasicBlock *bb =
379 (BasicBlock *) dvmGrowableListGetElement(blocks, i);
380 char blockName[BLOCK_NAME_LEN];
381 dvmGetBlockName(bb, blockName);
382 LOGE("Bit %d / %s is set", i, blockName);
383 }
384 }
385}
386
387void dvmGetBlockName(BasicBlock *bb, char *name)
388{
389 switch (bb->blockType) {
Ben Cheng32115a92011-03-22 14:09:09 -0700390 case kEntryBlock:
Ben Cheng00603072010-10-28 11:13:58 -0700391 snprintf(name, BLOCK_NAME_LEN, "entry");
392 break;
Ben Cheng32115a92011-03-22 14:09:09 -0700393 case kExitBlock:
Ben Cheng00603072010-10-28 11:13:58 -0700394 snprintf(name, BLOCK_NAME_LEN, "exit");
395 break;
396 case kDalvikByteCode:
397 snprintf(name, BLOCK_NAME_LEN, "block%04x", bb->startOffset);
398 break;
Ben Cheng46cd4fb2011-03-16 17:19:06 -0700399 case kChainingCellNormal:
400 snprintf(name, BLOCK_NAME_LEN, "chain%04x", bb->startOffset);
401 break;
Ben Cheng00603072010-10-28 11:13:58 -0700402 case kExceptionHandling:
403 snprintf(name, BLOCK_NAME_LEN, "exception%04x", bb->startOffset);
404 break;
405 default:
406 snprintf(name, BLOCK_NAME_LEN, "??");
407 break;
408 }
409}