summaryrefslogtreecommitdiff
path: root/vm/compiler/Ralloc.c
blob: f227527945110900faa976832940419308a20aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Dalvik.h"
#include "CompilerInternals.h"
#include "Dataflow.h"

typedef struct LiveRange {
    int ssaName;
    bool active;
    int first;
    int last;
} LiveRange;

int computeLiveRange(LiveRange *list, BasicBlock *bb, int seqNum)
{
    MIR *mir;
    int i;

    if (bb->blockType != kDalvikByteCode &&
        bb->blockType != kTraceEntryBlock)
        return seqNum;

    for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
        SSARepresentation *ssaRep = mir->ssaRep;
        mir->seqNum = seqNum;
        if (ssaRep) {
            for (i=0; i< ssaRep->numUses; i++) {
                int reg = ssaRep->uses[i];
                list[reg].first = MIN(list[reg].first, seqNum);
                list[reg].active = true;
            }
            for (i=0; i< ssaRep->numDefs; i++) {
                int reg = ssaRep->defs[i];
                list[reg].last = MAX(list[reg].last, seqNum + 1);
                list[reg].active = true;
            }
            seqNum += 2;
        }
    }
    return seqNum;
}

/*
 * Quick & dirty - make FP usage sticky.  This is strictly a hint - local
 * code generation will handle misses.  It might be worthwhile to collaborate
 * with dx/dexopt to avoid reusing the same Dalvik temp for values of
 * different types.
 */
static void inferTypes(CompilationUnit *cUnit, BasicBlock *bb)
{
    MIR *mir;
    if (bb->blockType != kDalvikByteCode &&
        bb->blockType != kTraceEntryBlock)
        return;

    for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
        SSARepresentation *ssaRep = mir->ssaRep;
        if (ssaRep) {
            int i;
            for (i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
                if (ssaRep->fpUse[i])
                    cUnit->regLocation[ssaRep->uses[i]].fp = true;
            }
            for (i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
                if (ssaRep->fpDef[i])
                    cUnit->regLocation[ssaRep->defs[i]].fp = true;
            }
        }
    }
}

/*
 * Determine whether to use simple or aggressive register allocation.  In
 * general, loops and full methods will get aggressive.
 */
static bool simpleTrace(CompilationUnit *cUnit)
{
    //TODO: flesh out
    return true;
}

/*
 * Target-independent register allocation.  Requires target-dependent
 * helper functions and assumes free list, temp list and spill region.
 * Uses a variant of linear scan and produces a mapping between SSA names
 * and location.  Location may be original Dalvik register, hardware
 * register or spill location.
 *
 * Method:
 *    0.  Allocate the structure to hold the SSA name life ranges
 *    1.  Number each MIR instruction, counting by 2.
 *        +0 -> The "read" of the operands
 *        +1 -> The definition of the target resource
 *    2.  Compute live ranges for all SSA names *not* including the
 *        subscript 0 original Dalvik names.  Phi functions ignored
 *        at this point.
 *    3.  Sort the live range list by lowest range start.
 *    4.  Process and remove all Phi functions.
 *        o If there is no live range collisions among all operands and
 *          the target of a Phi function, collapse operands and target
 *          and rewrite using target SSA name.
 *        o If there is a collision, introduce copies.
 *    5.  Allocate in order of increasing live range start.
 */
static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, INVALID_REG,
                                     INVALID_REG, INVALID_SREG};
void dvmCompilerRegAlloc(CompilationUnit *cUnit)
{
    int i;
    int seqNum = 0;
    LiveRange *ranges;
    RegLocation *loc;

    /* Allocate the location map */
    loc = (RegLocation*)dvmCompilerNew(cUnit->numSSARegs * sizeof(*loc), true);
    for (i=0; i< cUnit->numSSARegs; i++) {
        loc[i] = freshLoc;
        loc[i].sRegLow = i;
    }
    cUnit->regLocation = loc;

    /* Do type inference pass */
    for (i=0; i < cUnit->numBlocks; i++) {
        inferTypes(cUnit, cUnit->blockList[i]);
    }

    if (simpleTrace(cUnit)) {
        /*
         * Just rename everything back to subscript 0 names and don't do
         * any explicit promotion.  Local allocator will opportunistically
         * promote on the fly.
         */
        for (i=0; i < cUnit->numSSARegs; i++) {
            cUnit->regLocation[i].sRegLow =
                DECODE_REG(dvmConvertSSARegToDalvik(cUnit, loc[i].sRegLow));
        }
    } else {
        // Compute live ranges
        ranges = dvmCompilerNew(cUnit->numSSARegs * sizeof(*ranges), true);
        for (i=0; i < cUnit->numSSARegs; i++)
            ranges[i].active = false;
        seqNum = computeLiveRange(ranges, cUnit->blockList[i], seqNum);
        //TODO: phi squash & linear scan promotion
    }
}