2022-12-14 00:16:59 +00:00
|
|
|
#include "compiler/LiveInfo.h"
|
|
|
|
#include "compiler/BitVectors.h"
|
|
|
|
#include "compiler/Coloring.h"
|
|
|
|
#include "compiler/CompilerTools.h"
|
|
|
|
#include "compiler/PCode.h"
|
|
|
|
#include "compiler/Registers.h"
|
|
|
|
#include "compiler/objects.h"
|
|
|
|
#include "compiler/types.h"
|
|
|
|
#include "compiler/CParser.h"
|
|
|
|
|
|
|
|
LiveInfo *liveinfo;
|
|
|
|
|
|
|
|
static void allocateliveinfo(void) {
|
|
|
|
UInt32 regs;
|
|
|
|
LiveInfo *info;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
regs = used_virtual_registers[coloring_class];
|
|
|
|
|
|
|
|
liveinfo = oalloc(sizeof(LiveInfo) * pcblockcount);
|
|
|
|
for (i = 0, info = liveinfo; i < pcblockcount; i++, info++) {
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorinitialize(info->use = oalloc(4 * ((regs + 31) >> 5)), regs, 0);
|
|
|
|
bitvectorinitialize(info->def = oalloc(4 * ((regs + 31) >> 5)), regs, 0);
|
|
|
|
bitvectorinitialize(info->in = oalloc(4 * ((regs + 31) >> 5)), regs, 0);
|
|
|
|
bitvectorinitialize(info->out = oalloc(4 * ((regs + 31) >> 5)), regs, 0);
|
2022-12-14 00:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void computelocalusedef(void) {
|
|
|
|
LiveInfo *info;
|
|
|
|
PCodeBlock *block;
|
|
|
|
PCode *instr;
|
2022-12-14 01:00:56 +00:00
|
|
|
UInt32 *use;
|
|
|
|
UInt32 *def;
|
2022-12-14 00:16:59 +00:00
|
|
|
PCodeArg *op;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (block = pcbasicblocks; block; block = block->nextBlock) {
|
|
|
|
info = &liveinfo[block->blockIndex];
|
2022-12-14 01:00:56 +00:00
|
|
|
use = info->use;
|
|
|
|
def = info->def;
|
2022-12-14 00:16:59 +00:00
|
|
|
|
|
|
|
for (instr = block->firstPCode; instr; instr = instr->nextPCode) {
|
|
|
|
op = instr->args;
|
|
|
|
i = instr->argCount;
|
|
|
|
while (i--) {
|
2022-12-14 01:00:56 +00:00
|
|
|
if (PC_OP_IS_READ_ANY_REGISTER(op, coloring_class) && !bitvectorgetbit(op->data.reg.reg, def))
|
|
|
|
bitvectorsetbit(op->data.reg.reg, use);
|
2022-12-14 00:16:59 +00:00
|
|
|
op++;
|
|
|
|
}
|
|
|
|
|
|
|
|
op = instr->args;
|
|
|
|
i = instr->argCount;
|
|
|
|
while (i--) {
|
2022-12-14 01:00:56 +00:00
|
|
|
if (PC_OP_IS_WRITE_ANY_REGISTER(op, coloring_class) && !bitvectorgetbit(op->data.reg.reg, use))
|
|
|
|
bitvectorsetbit(op->data.reg.reg, def);
|
2022-12-14 00:16:59 +00:00
|
|
|
op++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void computeglobalinout(void) {
|
|
|
|
UInt32 regs;
|
|
|
|
LiveInfo *info;
|
2022-12-14 01:00:56 +00:00
|
|
|
UInt32 *use;
|
|
|
|
UInt32 *def;
|
|
|
|
UInt32 *in;
|
|
|
|
UInt32 *out;
|
2022-12-14 00:16:59 +00:00
|
|
|
int bitvecsize;
|
|
|
|
int blockIndex;
|
|
|
|
int i;
|
|
|
|
int flag;
|
|
|
|
PCodeBlock *block;
|
|
|
|
PCLink *link;
|
|
|
|
UInt32 val;
|
|
|
|
|
|
|
|
regs = used_virtual_registers[coloring_class];
|
|
|
|
bitvecsize = (regs + 31) >> 5;
|
|
|
|
flag = 1;
|
|
|
|
while (flag) {
|
|
|
|
flag = 0;
|
|
|
|
blockIndex = pcblockcount;
|
|
|
|
while (blockIndex) {
|
|
|
|
if ((block = depthfirstordering[--blockIndex])) {
|
|
|
|
info = &liveinfo[block->blockIndex];
|
|
|
|
if ((link = block->successors)) {
|
2022-12-14 01:00:56 +00:00
|
|
|
out = info->out;
|
|
|
|
bitvectorcopy(out, liveinfo[link->block->blockIndex].in, regs);
|
2022-12-14 00:16:59 +00:00
|
|
|
for (link = link->nextLink; link; link = link->nextLink)
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorunion(out, liveinfo[link->block->blockIndex].in, regs);
|
2022-12-14 00:16:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:00:56 +00:00
|
|
|
out = info->out;
|
|
|
|
in = info->in;
|
|
|
|
use = info->use;
|
|
|
|
def = info->def;
|
2022-12-14 00:16:59 +00:00
|
|
|
for (i = 0; i < bitvecsize; i++) {
|
2022-12-14 01:00:56 +00:00
|
|
|
val = *use | (*out & ~*def);
|
|
|
|
if (val != *in) {
|
|
|
|
*in = val;
|
2022-12-14 00:16:59 +00:00
|
|
|
flag = 1;
|
|
|
|
}
|
2022-12-14 01:00:56 +00:00
|
|
|
in++;
|
|
|
|
out++;
|
|
|
|
use++;
|
|
|
|
def++;
|
2022-12-14 00:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void computelivevariables(Object *proc) {
|
|
|
|
Type *returnType;
|
|
|
|
|
|
|
|
returnType = TYPE_FUNC(proc->type)->functype;
|
|
|
|
computedepthfirstordering();
|
|
|
|
allocateliveinfo();
|
|
|
|
computelocalusedef();
|
|
|
|
|
|
|
|
if (coloring_class == RegClass_GPR && TYPE_FITS_IN_REGISTER(returnType)) {
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorsetbit(3, liveinfo[epilogue->blockIndex].use);
|
2022-12-14 00:16:59 +00:00
|
|
|
if (TYPE_IS_8BYTES(returnType))
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorsetbit(4, liveinfo[pclastblock->blockIndex].use);
|
2022-12-14 00:16:59 +00:00
|
|
|
} else if (coloring_class == RegClass_FPR && IS_TYPE_FLOAT(returnType)) {
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorsetbit(1, liveinfo[epilogue->blockIndex].use);
|
2022-12-14 00:16:59 +00:00
|
|
|
} else if (coloring_class == RegClass_VR && IS_TYPE_VECTOR(returnType)) {
|
2022-12-14 01:00:56 +00:00
|
|
|
bitvectorsetbit(2, liveinfo[epilogue->blockIndex].use);
|
2022-12-14 00:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
computeglobalinout();
|
|
|
|
}
|
|
|
|
|
2022-12-14 01:00:56 +00:00
|
|
|
int dead(PCode *instr, RegClass rclass, UInt32 *vec) {
|
2022-12-14 00:16:59 +00:00
|
|
|
int i;
|
|
|
|
PCodeArg *op;
|
|
|
|
|
|
|
|
if (instr->flags & (fPCodeFlag1 | fPCodeFlag4 | fPCodeFlag8 | fIsVolatile | fSideEffects))
|
|
|
|
return 0;
|
|
|
|
if (instr->block->flags & (fPCBlockFlag1 | fPCBlockFlag2))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
op = instr->args;
|
|
|
|
i = instr->argCount;
|
|
|
|
while (i--) {
|
|
|
|
if (
|
|
|
|
op->kind == PCOp_REGISTER &&
|
|
|
|
(op->data.reg.effect & EffectWrite) &&
|
2022-12-14 01:00:56 +00:00
|
|
|
(rclass != op->arg || bitvectorgetbit(op->data.reg.reg, vec))
|
2022-12-14 00:16:59 +00:00
|
|
|
)
|
|
|
|
return 0;
|
|
|
|
op++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copts.optimizationlevel > 0;
|
|
|
|
}
|