| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | #include "Dataflow.h" |
| 20 | |
| 21 | typedef struct LiveRange { |
| 22 | int ssaName; |
| 23 | bool active; |
| 24 | int first; |
| 25 | int last; |
| 26 | } LiveRange; |
| 27 | |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 28 | /* |
| 29 | * Quick & dirty - make FP usage sticky. This is strictly a hint - local |
| 30 | * code generation will handle misses. It might be worthwhile to collaborate |
| 31 | * with dx/dexopt to avoid reusing the same Dalvik temp for values of |
| 32 | * different types. |
| 33 | */ |
| 34 | static void inferTypes(CompilationUnit *cUnit, BasicBlock *bb) |
| 35 | { |
| 36 | MIR *mir; |
| 37 | if (bb->blockType != kDalvikByteCode && |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 38 | bb->blockType != kTraceEntryBlock) |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 39 | return; |
| 40 | |
| 41 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 42 | SSARepresentation *ssaRep = mir->ssaRep; |
| 43 | if (ssaRep) { |
| 44 | int i; |
| 45 | for (i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) { |
| 46 | if (ssaRep->fpUse[i]) |
| 47 | cUnit->regLocation[ssaRep->uses[i]].fp = true; |
| 48 | } |
| 49 | for (i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) { |
| 50 | if (ssaRep->fpDef[i]) |
| 51 | cUnit->regLocation[ssaRep->defs[i]].fp = true; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Determine whether to use simple or aggressive register allocation. In |
| 59 | * general, loops and full methods will get aggressive. |
| 60 | */ |
| 61 | static bool simpleTrace(CompilationUnit *cUnit) |
| 62 | { |
| 63 | //TODO: flesh out |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Target-independent register allocation. Requires target-dependent |
| 69 | * helper functions and assumes free list, temp list and spill region. |
| 70 | * Uses a variant of linear scan and produces a mapping between SSA names |
| 71 | * and location. Location may be original Dalvik register, hardware |
| 72 | * register or spill location. |
| 73 | * |
| 74 | * Method: |
| 75 | * 0. Allocate the structure to hold the SSA name life ranges |
| 76 | * 1. Number each MIR instruction, counting by 2. |
| 77 | * +0 -> The "read" of the operands |
| 78 | * +1 -> The definition of the target resource |
| 79 | * 2. Compute live ranges for all SSA names *not* including the |
| 80 | * subscript 0 original Dalvik names. Phi functions ignored |
| 81 | * at this point. |
| 82 | * 3. Sort the live range list by lowest range start. |
| 83 | * 4. Process and remove all Phi functions. |
| 84 | * o If there is no live range collisions among all operands and |
| 85 | * the target of a Phi function, collapse operands and target |
| 86 | * and rewrite using target SSA name. |
| 87 | * o If there is a collision, introduce copies. |
| 88 | * 5. Allocate in order of increasing live range start. |
| 89 | */ |
| 90 | static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, INVALID_REG, |
| 91 | INVALID_REG, INVALID_SREG}; |
| 92 | void dvmCompilerRegAlloc(CompilationUnit *cUnit) |
| 93 | { |
| 94 | int i; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 95 | LiveRange *ranges; |
| 96 | RegLocation *loc; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 97 | |
| 98 | /* Allocate the location map */ |
| 99 | loc = (RegLocation*)dvmCompilerNew(cUnit->numSSARegs * sizeof(*loc), true); |
| 100 | for (i=0; i< cUnit->numSSARegs; i++) { |
| 101 | loc[i] = freshLoc; |
| 102 | loc[i].sRegLow = i; |
| 103 | } |
| 104 | cUnit->regLocation = loc; |
| 105 | |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 106 | GrowableListIterator iterator; |
| 107 | |
| 108 | dvmGrowableListIteratorInit(&cUnit->blockList, &iterator); |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 109 | /* Do type inference pass */ |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 110 | while (true) { |
| 111 | BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator); |
| 112 | if (bb == NULL) break; |
| 113 | inferTypes(cUnit, bb); |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (simpleTrace(cUnit)) { |
| 117 | /* |
| 118 | * Just rename everything back to subscript 0 names and don't do |
| 119 | * any explicit promotion. Local allocator will opportunistically |
| 120 | * promote on the fly. |
| 121 | */ |
| 122 | for (i=0; i < cUnit->numSSARegs; i++) { |
| 123 | cUnit->regLocation[i].sRegLow = |
| 124 | DECODE_REG(dvmConvertSSARegToDalvik(cUnit, loc[i].sRegLow)); |
| 125 | } |
| 126 | } else { |
| 127 | // Compute live ranges |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 128 | ranges = (LiveRange *)dvmCompilerNew(cUnit->numSSARegs * sizeof(*ranges), true); |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 129 | for (i=0; i < cUnit->numSSARegs; i++) |
| 130 | ranges[i].active = false; |
| Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 131 | //TODO: phi squash & linear scan promotion |
| 132 | } |
| 133 | } |