use of org.jikesrvm.compilers.opt.ir.operand.RegisterOperand in project JikesRVM by JikesRVM.
the class LoopVersioning method generateNullCheckBranchBlocks.
/**
* Generate null check branch blocks
*
* @param loop the current loop where checks are being eliminated
* @param checksToEliminate all of the checks that are being eliminated in the pass
* @param optimalRegMap a map from original register to the register used in the optimal loop
* @param block the block to generate code into
* @param unoptimizedLoopEntry entry to the unoptimized loop for if the check fails
* @return the new block to generate code into
*/
private BasicBlock generateNullCheckBranchBlocks(AnnotatedLSTNode loop, ArrayList<Instruction> checksToEliminate, HashMap<Register, Register> optimalRegMap, BasicBlock block, BasicBlock unoptimizedLoopEntry) {
// Map of already generated null check references to their
// corresponding guard result
HashMap<Register, Operand> refToGuardMap = new HashMap<Register, Operand>();
// Iterate over checks
for (Instruction instr : checksToEliminate) {
// Is this a null check
if (NullCheck.conforms(instr)) {
// the generated branch instruction
Instruction branch;
// the reference to compare
Operand ref = AnnotatedLSTNode.follow(NullCheck.getRef(instr));
// the guard result to define
RegisterOperand guardResult = NullCheck.getGuardResult(instr).copyRO();
guardResult.setRegister(optimalRegMap.get(guardResult.getRegister()));
// check if we've generated this test already
if (ref.isRegister() && refToGuardMap.containsKey(ref.asRegister().getRegister())) {
// yes - generate just a guard move
branch = Move.create(GUARD_MOVE, guardResult, refToGuardMap.get(ref.asRegister().getRegister()).copy());
branch.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(branch);
} else {
// check if we can just move a guard from the loop predecessors
RegisterOperand guard = nullCheckPerformedInLoopPredecessors(loop.header, instr);
if (guard != null) {
// yes - generate just a guard move
branch = Move.create(GUARD_MOVE, guardResult, guard.copyRO());
branch.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(branch);
} else {
// generate explicit null test
branch = IfCmp.create(REF_IFCMP, guardResult, ref.copy(), new NullConstantOperand(), ConditionOperand.EQUAL(), unoptimizedLoopEntry.makeJumpTarget(), BranchProfileOperand.unlikely());
if (ref.isRegister()) {
refToGuardMap.put(ref.asRegister().getRegister(), guardResult);
}
branch.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(branch);
// Adjust block
block.insertOut(unoptimizedLoopEntry);
BasicBlock new_block = block.createSubBlock(SYNTH_LOOP_VERSIONING_BCI, ir);
BasicBlock temp = (BasicBlock) block.next;
ir.cfg.breakCodeOrder(block, temp);
ir.cfg.linkInCodeOrder(block, new_block);
ir.cfg.linkInCodeOrder(new_block, temp);
block.insertOut(new_block);
block = new_block;
}
}
}
}
return block;
}
use of org.jikesrvm.compilers.opt.ir.operand.RegisterOperand in project JikesRVM by JikesRVM.
the class LoopVersioning method createCloneLoop.
/**
* Create a clone of the loop replacing definitions in the cloned
* loop with those found in the register map
* @param loop - loop to clone
* @param regMap - mapping of original definition to new
* definition
* @param regToBlockMap mapping of registers to new, unoptimized blocks. This starts
* empty and will be filled during execution of the method.
* @return a mapping from original BBs to created BBs
*/
private HashMap<BasicBlock, BasicBlock> createCloneLoop(AnnotatedLSTNode loop, HashMap<Register, Register> regMap, HashMap<Register, BasicBlock> regToBlockMap) {
HashMap<BasicBlock, BasicBlock> originalToCloneBBMap = new HashMap<BasicBlock, BasicBlock>();
// After the newly created loop goto the old loop header
originalToCloneBBMap.put(loop.successor, loop.header);
// Create an empty block to be the loop predecessor
BasicBlock new_pred = loop.header.createSubBlock(SYNTH_LOOP_VERSIONING_BCI, ir);
ir.cfg.linkInCodeOrder(ir.cfg.lastInCodeOrder(), new_pred);
originalToCloneBBMap.put(loop.predecessor, new_pred);
// Create copy blocks
Enumeration<BasicBlock> blocks = loop.getBasicBlocks();
while (blocks.hasMoreElements()) {
BasicBlock block = blocks.nextElement();
// get rid of fall through edges to aid recomputeNormalOuts
block.killFallThrough();
// Create copy and register mapping
BasicBlock copy = block.copyWithoutLinks(ir);
originalToCloneBBMap.put(block, copy);
// Link into code order
ir.cfg.linkInCodeOrder(ir.cfg.lastInCodeOrder(), copy);
// Alter register definitions and uses in copy
IREnumeration.AllInstructionsEnum instructions = new IREnumeration.AllInstructionsEnum(ir, copy);
while (instructions.hasMoreElements()) {
Instruction instruction = instructions.nextElement();
Enumeration<Operand> operands = instruction.getDefs();
while (operands.hasMoreElements()) {
Operand operand = operands.nextElement();
if (operand.isRegister()) {
Register register = operand.asRegister().getRegister();
if (regMap.containsKey(register)) {
instruction.replaceRegister(register, regMap.get(register));
regToBlockMap.put(regMap.get(register), copy);
}
}
}
operands = instruction.getUses();
while (operands.hasMoreElements()) {
Operand operand = operands.nextElement();
if (operand instanceof RegisterOperand) {
Register register = operand.asRegister().getRegister();
if (regMap.containsKey(register)) {
instruction.replaceRegister(register, regMap.get(register));
}
}
}
}
}
// Fix up outs
// loop predecessor
new_pred.redirectOuts(loop.header, originalToCloneBBMap.get(loop.header), ir);
// loop blocks
blocks = loop.getBasicBlocks();
while (blocks.hasMoreElements()) {
BasicBlock block = blocks.nextElement();
BasicBlock copy = originalToCloneBBMap.get(block);
Enumeration<BasicBlock> outs = block.getOutNodes();
while (outs.hasMoreElements()) {
BasicBlock out = outs.nextElement();
if (originalToCloneBBMap.containsKey(out)) {
copy.redirectOuts(out, originalToCloneBBMap.get(out), ir);
}
}
}
// Fix up phis
blocks = loop.getBasicBlocks();
while (blocks.hasMoreElements()) {
BasicBlock block = blocks.nextElement();
BasicBlock copy = originalToCloneBBMap.get(block);
IREnumeration.AllInstructionsEnum instructions = new IREnumeration.AllInstructionsEnum(ir, copy);
while (instructions.hasMoreElements()) {
Instruction instruction = instructions.nextElement();
if (Phi.conforms(instruction)) {
for (int i = 0; i < Phi.getNumberOfValues(instruction); i++) {
BasicBlock phi_predecessor = Phi.getPred(instruction, i).block;
if (originalToCloneBBMap.containsKey(phi_predecessor)) {
Phi.setPred(instruction, i, new BasicBlockOperand(originalToCloneBBMap.get(phi_predecessor)));
} else {
dumpIR(ir, "Error when optimising" + ir.getMethod());
throw new Error("There's > 1 route to this phi node " + instruction + " from outside the loop: " + phi_predecessor);
}
}
}
}
}
return originalToCloneBBMap;
}
use of org.jikesrvm.compilers.opt.ir.operand.RegisterOperand in project JikesRVM by JikesRVM.
the class LoopVersioning method createBranchBlocks.
/*
* TODO better JavaDoc comment.
* <p>
* Create the block containing explict branches to either the
* optimized or unoptimized loops
* @param optimalRegMap - mapping used to map eliminated bound and
* null check guards to
*/
private boolean createBranchBlocks(AnnotatedLSTNode loop, BasicBlock block, ArrayList<Instruction> checksToEliminate, BasicBlock unoptimizedLoopEntry, BasicBlock optimizedLoopEntry, HashMap<Register, Register> optimalRegMap) {
BasicBlock blockOnEntry = block;
// 1) generate null check guards
block = generateNullCheckBranchBlocks(loop, checksToEliminate, optimalRegMap, block, unoptimizedLoopEntry);
// 2) generate bound check guards
if (loop.isMonotonic()) {
// create new operands for values beyond initial and terminal iterator values
Operand terminal;
Operand terminalLessStrideOnce;
Operand terminalPlusStrideOnce;
// it does create dead code though
if (loop.terminalIteratorValue.isIntConstant()) {
terminal = loop.terminalIteratorValue;
int terminalAsInt = terminal.asIntConstant().value;
int stride = loop.strideValue.asIntConstant().value;
terminalLessStrideOnce = new IntConstantOperand(terminalAsInt - stride);
terminalPlusStrideOnce = new IntConstantOperand(terminalAsInt + stride);
} else {
Instruction tempInstr;
terminal = loop.generateLoopInvariantOperand(block, loop.terminalIteratorValue);
terminalLessStrideOnce = ir.regpool.makeTempInt();
terminalPlusStrideOnce = ir.regpool.makeTempInt();
tempInstr = Binary.create(INT_SUB, terminalLessStrideOnce.asRegister(), terminal.copy(), loop.strideValue.copy());
tempInstr.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(tempInstr);
DefUse.updateDUForNewInstruction(tempInstr);
tempInstr = Binary.create(INT_ADD, terminalPlusStrideOnce.asRegister(), terminal.copy(), loop.strideValue.copy());
tempInstr.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(tempInstr);
DefUse.updateDUForNewInstruction(tempInstr);
}
// Determine maximum and minimum index values for different loop types
Operand phiMinIndexValue;
Operand phiMaxIndexValue;
if (loop.isMonotonicIncreasing()) {
phiMinIndexValue = loop.initialIteratorValue;
if ((loop.condition.isLESS() || loop.condition.isLOWER() || loop.condition.isNOT_EQUAL())) {
phiMaxIndexValue = terminal;
} else if ((loop.condition.isLESS_EQUAL() || loop.condition.isLOWER_EQUAL() || loop.condition.isEQUAL())) {
phiMaxIndexValue = terminalPlusStrideOnce;
} else {
throw new Error("Unrecognised loop for fission " + loop);
}
} else if (loop.isMonotonicDecreasing()) {
phiMaxIndexValue = loop.initialIteratorValue;
if ((loop.condition.isGREATER() || loop.condition.isHIGHER() || loop.condition.isNOT_EQUAL())) {
phiMinIndexValue = terminalPlusStrideOnce;
} else if ((loop.condition.isGREATER_EQUAL() || loop.condition.isHIGHER_EQUAL() || loop.condition.isEQUAL())) {
phiMinIndexValue = terminalLessStrideOnce;
} else {
throw new Error("Unrecognised loop for fission " + loop);
}
} else {
throw new Error("Unrecognised loop for fission " + loop);
}
// Generate tests
for (int i = 0; i < checksToEliminate.size(); i++) {
Instruction instr = checksToEliminate.get(i);
if (BoundsCheck.conforms(instr)) {
// Have we already generated these tests?
boolean alreadyChecked = false;
for (int j = 0; j < i; j++) {
Instruction old_instr = checksToEliminate.get(j);
if (BoundsCheck.conforms(old_instr) && (BoundsCheck.getRef(old_instr).similar(BoundsCheck.getRef(instr))) && (BoundsCheck.getIndex(old_instr).similar(BoundsCheck.getIndex(instr)))) {
// yes - just create a guard move
alreadyChecked = true;
RegisterOperand guardResult = BoundsCheck.getGuardResult(instr).copyRO();
guardResult.setRegister(optimalRegMap.get(guardResult.getRegister()));
RegisterOperand guardSource = BoundsCheck.getGuardResult(old_instr).copyRO();
guardSource.setRegister(optimalRegMap.get(guardSource.getRegister()));
Instruction tempInstr = Move.create(GUARD_MOVE, guardResult, guardSource);
tempInstr.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(tempInstr);
break;
}
}
if (!alreadyChecked) {
// no - generate tests
Operand index = BoundsCheck.getIndex(instr);
int distance = loop.getFixedDistanceFromPhiIterator(index);
if (distance == 0) {
block = generateExplicitBoundCheck(instr, phiMinIndexValue, phiMaxIndexValue, optimalRegMap, block, unoptimizedLoopEntry);
} else {
Instruction tempInstr;
RegisterOperand minIndex = ir.regpool.makeTempInt();
RegisterOperand maxIndex = ir.regpool.makeTempInt();
tempInstr = Binary.create(INT_ADD, minIndex, phiMinIndexValue.copy(), new IntConstantOperand(distance));
tempInstr.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(tempInstr);
DefUse.updateDUForNewInstruction(tempInstr);
tempInstr = Binary.create(INT_ADD, maxIndex, phiMaxIndexValue.copy(), new IntConstantOperand(distance));
tempInstr.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(tempInstr);
DefUse.updateDUForNewInstruction(tempInstr);
block = generateExplicitBoundCheck(instr, minIndex, maxIndex, optimalRegMap, block, unoptimizedLoopEntry);
}
}
}
}
}
// Have we had to create a new basic block since entry => we
// generated a branch to the unoptimized loop
boolean isUnoptimizedLoopReachable = (blockOnEntry != block);
// 3) Finish up with goto and generate true guard value
{
// the generated branch instruction
Instruction branch;
branch = Goto.create(GOTO, optimizedLoopEntry.makeJumpTarget());
branch.setBytecodeIndex(SYNTH_LOOP_VERSIONING_BCI);
block.appendInstruction(branch);
block.deleteNormalOut();
block.insertOut(optimizedLoopEntry);
}
return isUnoptimizedLoopReachable;
}
use of org.jikesrvm.compilers.opt.ir.operand.RegisterOperand in project JikesRVM by JikesRVM.
the class PiNodes method insertPiNullCheckNodes.
/**
* Insert Pi nodes for null check operations.
*
* <p>Each checkcast obj will be followed by
* <pre> PI obj, obj </pre>
*
* @param ir the governing IR
*/
private void insertPiNullCheckNodes(IR ir) {
if (!CHECK_REF_PI)
return;
Instruction nextInst = null;
// for each instruction in the IR
for (Instruction instr = ir.firstInstructionInCodeOrder(); instr != null; instr = nextInst) {
// can't use iterator, since we modify instruction stream
nextInst = instr.nextInstructionInCodeOrder();
if (NullCheck.conforms(instr)) {
// get compared variables
Operand obj = NullCheck.getRef(instr);
// create the instruction and insert it
if (obj.isRegister()) {
RegisterOperand lval = (RegisterOperand) obj.copy();
Instruction s = GuardedUnary.create(PI, lval, obj.copy(), null);
RegisterOperand sGuard = (RegisterOperand) NullCheck.getGuardResult(instr).copy();
sGuard.setNullCheck();
GuardedUnary.setGuard(s, sGuard);
instr.insertAfter(s);
}
}
}
}
use of org.jikesrvm.compilers.opt.ir.operand.RegisterOperand in project JikesRVM by JikesRVM.
the class PiNodes method insertPiCheckCastNodes.
/**
* Insert Pi nodes for checkcast operations.
*
* <p>Each checkcast obj will be followed by
* <pre> ref_move obj, obj </pre>
*
* @param ir the governing IR
*/
private void insertPiCheckCastNodes(IR ir) {
Instruction nextInst = null;
// for each instruction in the IR
for (Instruction instr = ir.firstInstructionInCodeOrder(); instr != null; instr = nextInst) {
// can't use iterator, since we modify instruction stream
nextInst = instr.nextInstructionInCodeOrder();
if (TypeCheck.conforms(instr)) {
// get compared variables
Operand obj = TypeCheck.getRef(instr);
// create the instruction and insert it
if (obj.isRegister()) {
RegisterOperand lval = (RegisterOperand) obj.copy();
lval.clearDeclaredType();
if (lval.getType().isLoaded() && lval.getType().isClassType() && lval.getType().peekType().asClass().isFinal()) {
lval.setPreciseType(TypeCheck.getType(instr).getTypeRef());
} else {
lval.clearPreciseType();
lval.setType(TypeCheck.getType(instr).getTypeRef());
}
Instruction s = GuardedUnary.create(PI, lval, obj.copy(), null);
s.copyPosition(instr);
Operand iGuard = TypeCheck.getGuard(instr);
if (iGuard != null) {
Operand sGuard = iGuard.copy();
GuardedUnary.setGuard(s, sGuard);
}
instr.insertAfter(s);
}
}
}
}
Aggregations