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/*
22 * Quick & dirty - make FP usage sticky.  This is strictly a hint - local
23 * code generation will handle misses.  It might be worthwhile to collaborate
24 * with dx/dexopt to avoid reusing the same Dalvik temp for values of
25 * different types.
26 */
27static void inferTypes(CompilationUnit *cUnit, BasicBlock *bb)
28{
29    MIR *mir;
30    if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
31        return;
32
33    for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
34        SSARepresentation *ssaRep = mir->ssaRep;
35        if (ssaRep) {
36            int i;
37            for (i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
38                if (ssaRep->fpUse[i])
39                    cUnit->regLocation[ssaRep->uses[i]].fp = true;
40            }
41            for (i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
42                if (ssaRep->fpDef[i])
43                    cUnit->regLocation[ssaRep->defs[i]].fp = true;
44            }
45        }
46    }
47}
48
49static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, INVALID_REG,
50                                     INVALID_REG, INVALID_SREG};
51
52/*
53 * Local register allocation for simple traces.  Most of the work for
54 * local allocation is done on the fly.  Here we do some initialization
55 * and type inference.
56 */
57void dvmCompilerLocalRegAlloc(CompilationUnit *cUnit)
58{
59    int i;
60    RegLocation *loc;
61
62    /* Allocate the location map */
63    loc = (RegLocation*)dvmCompilerNew(cUnit->numSSARegs * sizeof(*loc), true);
64    for (i=0; i< cUnit->numSSARegs; i++) {
65        loc[i] = freshLoc;
66        loc[i].sRegLow = i;
67    }
68    cUnit->regLocation = loc;
69
70    GrowableListIterator iterator;
71
72    dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
73    /* Do type inference pass */
74    while (true) {
75        BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
76        if (bb == NULL) break;
77        inferTypes(cUnit, bb);
78    }
79
80    /* Remap SSA names back to original frame locations. */
81    for (i=0; i < cUnit->numSSARegs; i++) {
82        cUnit->regLocation[i].sRegLow =
83                DECODE_REG(dvmConvertSSARegToDalvik(cUnit, loc[i].sRegLow));
84    }
85}
86